Javascript/jQuery HasLoaded or equivalent?

点点圈 提交于 2019-12-11 07:13:53

问题


I know in jquery it is possible to call the javascript/jquery onload()/load() functions on, for example an image (<img>).

However, if in jquery if i use .html(htmlString) to insert an image after the dom has loaded, how can i add a listener to handle the images onload event? Is there a property I can check to see various images and if they have loaded?


回答1:


After appending your html, you can bind the load event to the contained images:

$("#foo").html(htmlString).find("img").one("load", function() {
    ...
}).each(function() {

    // image has been cached, so load event won't fire unless we explicitly call it
    if(this.complete) $(this).trigger("load");
});



回答2:


Check the complete property of the image(s)



来源:https://stackoverflow.com/questions/2850011/javascript-jquery-hasloaded-or-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!