简体   繁体   English

如何检查是否已完全加载对innerHTML的更改

[英]How to check if changes to innerHTML have completely loaded

I'm changing the content of a div via javascript using myDiv.innerHTML = xmlHttp.responseText . 我正在使用myDiv.innerHTML = xmlHttp.responseText通过javascript更改div的内容。 xmlHttp.responseText might contain a few images which will be loaded after the div's innerHTML has been changed. xmlHttp.responseText可能包含一些图像,这些图像将在div的innerHTML更改后加载。

Is there any event which is fired on the div, document or window after those images have completed loading ie after innerHTML of an element has completely loaded? 在这些图像完成加载之后,即在元素的innerHTML完全加载之后,是否会在div,文档或窗口上触发任何事件?

Well, I went through your question several times again and I came up with this process : 好吧,我再次经历了您的问题几次,然后想到了这个过程:

http://jsfiddle.net/Bladepianist/oyv0f7ns/ http://jsfiddle.net/Bladepianist/oyv0f7ns/

HTML HTML

<div id="myDiv">Click Me</div>
<div id="myDiv2">Watch</div>

I used two div so that you could observe the loading of differents images. 我使用了两个div以便您可以观察差异图像的加载。

JS (No JQuery) JS(无JQuery)

var myDiv = document.getElementById("myDiv");
var myDiv2 = document.getElementById("myDiv2");

var myWatcher = 0;

myDiv.onclick = function () {

    var myImage = "<img id='myImage' src='http://getbootstrap.com/assets/img/sass-less.png' style='max-width: 150px;' />";

    var myImage2 = "<img id='myImage2' src='http://getbootstrap.com/assets/img/devices.png' style='max-width: 150px;'  />";

    myDiv.innerHTML = myImage;
    myDiv2.innerHTML = myImage2;

    // Most important part. Get all the img tags AFTER div was modified.
    var myImg = document.getElementsByTagName("img");

    // Check if each img is loaded. Second img might be loaded before first.
    for (var i = 0; i < myImg.length; i++) {

        myImg[i].onload = function () {
            myWatcher++;

            if (myWatcher == myImg.length) {
                alert("All is loaded.");
                // Might want to call a function for anything you might want to do.
            }
        };
    }
};

In my example, I used a click event but the most important part is, of course, the capture of all your img element right after you modify your DOM. 在我的示例中,我使用了click event但是最重​​要的部分当然是在修改DOM之后立即捕获所有img元素。

The for loop allow the add an eventListener (here : onload ) on each img so that you know when it's loaded. for循环允许在每个img上添加一个eventListener(在这里: onload ),以便您知道何时加载它。

I have a counter which is incremented when an img is loaded. 我有一个计数器,在加载img时会增加。 When he is equal to myImg.length it means all's good. 当他等于myImg.length ,表示一切都很好。

PS (Edit) : Haven't tested on other browsers (just Chrome). PS(编辑):尚未在其他浏览器(仅Chrome)上进行测试。

Hope it might help you, even by a little. 希望它可以对您有所帮助,即使有一点帮助。

One way would be to search though responseText for <img> and add onload="function()" . 一种方法是通过responseText搜索<img>并添加onload="function()" You could do that with responseText.replace(/<img/g,"<img onload=\\"function\\"") . 您可以使用responseText.replace(/<img/g,"<img onload=\\"function\\"")来做到这一点。

As the comments on the question suggested, another way would be to loop through myDiv.getElementsByTagName("img") and add an event listener to the "load" event for each one. 正如对该问题的评论所建议的那样,另一种方法是遍历myDiv.getElementsByTagName("img")并将事件侦听器添加到每个事件的"load"事件。

There is an event, which is fired after the images are completely loaded. 有一个事件,在完全加载图像后将触发该事件。 So in your ajax request, you can set up an event listener: 因此,在您的ajax请求中,您可以设置一个事件侦听器:

xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4 && xmlhttp.status==200) {

      // readyState==4 means "request finished and response is ready"
      // status==200 means "everything transferred correctly"

    }
}

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

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