DOMContentLoaded我最初使用with编写了一堆代码addEventListener,然后attachEvent,onreadystatechange/(doScroll)用于旧的 IE / Opera,并回退到window.onload = function(){...}. 但仔细想想,似乎有一个更简单的方法:只需检查 if document.bodyis not nullor undefined。除了没有Event可用的对象之外,使用这种方法还有什么注意事项吗?因为到目前为止,这作为一种简单的方法非常有效:
function checkReady(func) {
function checkBody() {
if (document.body) { clearTimeout(x); func.call(document) }
}
var x = setTimeout(checkBody,10);
}
checkReady(function() {
console.log(this.body.childNodes);
});