简体   繁体   中英

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

The problem is to check in javascript: if there is an image,to add the padding-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.

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