我有这个代码:
window.addEvent('domready', function() {
var li_list = document.getElementById("topmenu").getElementsByTagName("li");
for (var i=0; i<li_list.length; i++) {
li_list[i].onmouseover=function() {
this.className+=" hover";
}
li_list[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" hover\\b"), "");
}
}
})
在 IE7-8 中失败:document.getElementById(...) 为 null 或不是对象。
在 Firefox 中它运行良好。
影响网站主菜单功能的:http://paraguasparados.com
谢谢。
最佳答案
在 IE 上,domready 可以在 dom 真正准备好之前触发。 Post.
因此,当您的代码执行时,document
对象尚不可用。 (因此 IE 错误消息“document.getElementById(...) 为 null 或不是对象。”)
解决方案:使用工具包(jQuery、yui 等)提供可在 IE 和其他浏览器上运行的 domready 的等效项。
关于javascript - 为什么这段 Javascript 代码在 IE7-8 中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7375889/