0

这是我的代码:

document.addEventListener('load', function () {
  alert(document.getElementsByTagName("DIV").length);
}, false);
//'load' event above doesn't show any response, alert isn't showing

alert(document.getElementsByTagName("DIV").length);
// this alert returns 0 it looks like it is called before the page DOM has loaded

window.onload = function() {
 alert(document.getElementsByTagName("DIV").length);
};
//returns 0, what the... it seems DOM hasn't loaded also
// but only on some sites, here on stackoverflow and youtube it works,
//but on google.com and many other websites (pcworld.com) shows 0

在最新的稳定版和 alpha 版 Operas 中也是如此。

4

1 回答 1

0

我建议你简单地做

window.addEventListener('load', function(){}, false)

就像在普通脚本中一样。您可以使用 opera.addEventListener('BeforeEvent.load', ...),但如果页面的脚本在某些 Opera 版本中不侦听加载事件,则可能不会触发。

其他一些背景阅读: window.onload vs document.onload

addEventListener("input", callback) 在歌剧中不起作用?

于 2012-01-03T10:59:11.937 回答