繁体   English   中英

为null或不是使用javascript的IE8 / 7中的对象

[英]is null or not an object in IE8/7 by using javascript

JavaScript是这样的:

    <script type="text/javascript">
var links = document.getElementById("practice_nav_var_color");
var a = links.getElementsByTagName("a");
var thisLocationHref = window.location.href;
for(var i=0;i<a.length;i++){ 

  var tempLink = a[i];      

  if(thisLocationHref === tempLink.href)
  {
      tempLink.style.backgroundColor="#7387a2";
  }
  else
  {
      tempLink.style.backgroundColor="#8d8679";
  }
}

在IE8中,错误消息显示“链接”为null或不是对象。
所以我的第一个猜测是IE不喜欢document.getElementById ...

我在网上搜索过,似乎它在div之前有条件语句,我不确定这是什么意思。

欢迎任何帮助。 感谢您的时间!

尝试这个

var links = document.getElementById("practice_nav_var_color");
if(links) {
    var a = links.getElementsByTagName("a");
    var thisLocationHref = window.location.href;
    for(var i=0;i<a.length;i++){ 

          var tempLink = a[i];      

        if(thisLocationHref === tempLink.href)
        {
            tempLink.style.backgroundColor="#7387a2";
        }
        else
        {
            tempLink.style.backgroundColor="#8d8679";
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM