DOM更改后捕获window.onload

时间:2015-11-25 13:28:13

标签: javascript image dom onload

我希望在DOM更改后加载所有图像时收到通知。我知道有一个jQuery插件,但我想知道是否有任何原生方法。

不起作用的代码示例: https://jsfiddle.net/wanpd5su/3/

// Real implementation will do an AJAX request to retrieve new
// document content. This one just sets a timeout instead.
$('#button').on('click', function doAjaxRequest() {
    // This function changes the document content so that it
    // contains new uncached images.
    window.setTimeout(function changeContent() {
        document.body.innerHTML = '<div id="div" style="background-image:url(\'http://lorempixel.com/800/600/abstract/?' + Math.random() + '\'); height:600px; width:800px;"></div>';
        // This handler should run when new conent
        // is ready and all images are loaded.
        // BOT IT DOES NOT :(
        $(window).on('load', function onLoad() {
            alert('Document updated successfully.');
        });
    }, 1000);
});

0 个答案:

没有答案