开发者

.each() method only applies to first element in IE

I have a jQuery like so:

$('#corner .photo img').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 in all browsers except in IE 7 and 8. In those brows开发者_如何学运维ers, it only applies the new property to the first element of #corner (which is the first .photo img).

If I remove #corner, it applies to all instances of ".photo img" inside it. However, I cannot remove the id #corner because some other parts of the HTML are using ".photo img" and I don't want this jQuery script to be applied to all. I only want it to be applied inside #corner.

Is there a way to solve this?

EDIT:

My bad, apparently there are multiple divs with the same name in the document, which are causing the problems in IE. I changed them to classes and it now works.


You can try

$('#corner').find('.photo img').each(...

By the way, IE does not like multiple items with the same ID. If you are using it this way with multiple id="corner" items them change that to a class and you should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜