javascript - 为什么这段 Javascript 代码在 IE7-8 中失败?

标签 javascript internet-explorer

我有这个代码:

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/

相关文章:

javascript - object.addEventListener 的 useCapture 参数的用途是什么?

javascript - 如何停止 Node js-winston库中出现audit.json文件

javascript - jQuery 验证 : Form is Being Validated Twice

javascript - D3 js 在快速鼠标单击时出现意外行为

javascript - 是否可以禁用 html 中所需的 ='required' 验证?

html - 一个页面上有多个 X-UA-Compatible 元标记

javascript - 平滑切换滑动(向上)问题

css - fontawesome 仅在 IE 中不起作用

javascript - 由于某种原因,图像未在 IE 8 或 IE 9 中显示

javascript - 强制网页以 100% 缩放打开(在 IE8 中)