开发者

Jquery: .onload() function doesn't work in IE

The problem is to check in javascript: if there is an image,to add the 开发者_如何学Gopadding-left. So in Chrom, Mazilla, Opera that works fine, but onload function doesn't work in IE7/8, the rest of the script also works fine.

    img.onload = function () {
      $('#text_near_img').css('padding-left', 162);
};

$('#text_near_img').css('width', 300);

Thanks for any ideas.


Try using:

$("img").load(function(){
    $('#text_near_img').css('padding-left', 162);
});

to allow jQuery to use it's more compatible methods.


Wrap your code in a document ready, so it does the check when dom is ready and check if image(s) exists:

$(function(){

if ($('img').length > 0)
{
      $('#text_near_img').css('padding-left', 162);
      $('#text_near_img').css('width', 300);
}

});

*The problem is that IE most probably is applying the css settings before the element has been added to stage, hence it will find nothing and do nothing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜