简体   繁体   English

jQuery脚本不适用于上传的图像

[英]jQuery script doesn't work on uploaded image

I have this jQuery code which centralizes an image within a DIV horizontally and vertically: 我有此jQuery代码,可将DIV中的图像水平和垂直集中在中央:

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). 最初应用于图像的jQuery代码不再起作用(图像不再集中化)。

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. 哦,这是JavaScript。 Remove the $ on the variables: itemImage instead of $itemImage, img instead of $img. 删除变量上的$:itemImage代替$ itemImage,img代替$ img。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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