2013-05-09 21 views
0

JavaScript的是這樣的:爲空或不是在IE8/7的目的是通過使用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,該錯誤消息表示,「鏈接」爲空或不是一個對象。
所以我的第一個猜測是IE不喜歡的document.getElementById ...

我在網上搜索,現在看來似乎具有條件語句以前到div,這我不知道那是什麼意思。

任何幫助,歡迎。感謝您的時間!

+0

什麼是你想要存檔什麼? – 2013-05-09 17:01:42

+0

我想保留代碼,但減少IE8中的錯誤消息。現在它每次打開或刷新頁面時都會彈出錯誤消息。 – Pluto 2013-05-09 17:05:01

+0

我的意思是,你想要用那個代碼存檔什麼,如果提醒某事,提交表單等等。代碼是什麼。 – 2013-05-09 17:06:35

回答

0

試試這個

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"; 
     } 
    } 
} 
相關問題