简体   繁体   中英

jQuery script doesn't work on uploaded image

I have this jQuery code which centralizes an image within a DIV horizontally and vertically:

var $itemImage = $('.a-img');
$itemImage.each(function() {
    var $img = $(this);
    var h = $img.height();
    var w = $img.width();
    $img.css('margin-top', +h / -2 + "px").css('margin-left',+ w/ -2 + "px");
});

It works when the page is first loaded. However, there's a link on the page that will show a pop-up box where you can update the photo. Once you click "Upload" on the pop-up box, the image on the main page will change without refreshing the entire page. The jQuery code that initially applied to the image no longer works (the image is no longer centralized).

How do I solve this problem?

Put this into a function, and call this function when you load the image. Oh, and this is JavaScript. Remove the $ on the variables: itemImage instead of $itemImage, img instead of $img.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM