2013-01-02 42 views
0

當我使用HTML#1時,多個ID可以正常工作,但在使用HTML#2時無法正常工作。 Chrome網絡控制檯顯示多個「document.getElementById」腳本不起作用

>> "Uncaught TypeError: Cannot call method 'setAttribute' of null ".

在HTML#2我刪除

<div id="0001"><a href="#"">LOGIN</a></div>,以及瀏覽器不工作了。

HTML#1

<html> 
<head> 
<title>Index</title> 
</head> 
<body> 

<div id="0001"><a href="#"">LOGIN</a></div> 
<div id="0002"><a href="#"">GO</a></div> 
<div id="0003"><a href="#"">VISIT</a></div> 
<div id="0004"><a href="#"">EXPLORE</a></div> 
<div id="0005"><a href="#"">FUNNY PICS</a></div> 
<script> 
var element = document.getElementById("0001"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/1.html\''); 

var element = document.getElementById("0002"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/2.html\''); 

var element = document.getElementById("0003"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/3.html\''); 

var element = document.getElementById("0004"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/4.html\''); 

var element = document.getElementById("0005"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/5.html\''); 
</script> 

</body> 
</html> 

HTML#2

<html> 
<head> 
<title>Index</title> 
</head> 
<body> 

<div id="0002"><a href="#"">GO</a></div> 
<div id="0003"><a href="#"">VISIT</a></div> 
<div id="0004"><a href="#"">EXPLORE</a></div> 
<div id="0005"><a href="#"">FUNNY PICS</a></div> 
<script> 
var element = document.getElementById("0001"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/1.html\''); 

var element = document.getElementById("0002"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/2.html\''); 

var element = document.getElementById("0003"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/3.html\''); 

var element = document.getElementById("0004"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/4.html\''); 

var element = document.getElementById("0005"); 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/5.html\''); 
</script> 

</body> 
</html> 
+2

您正在使用的ID刪除0001格,然後尋找它。因此它給出了錯誤。 –

+0

等待...你想使用'var元素= document.getElementById(「0001」); ...'當'#0001'確實存在** NOT **嗎? –

+0

僅供參考,標題爲SCRIPT的帖子無效 - 請修正它傾向於關閉SO社區。 –

回答

3

整體概念差。如果您有鏈接,請使用鏈接。直接無腳本(適合所有​​瀏覽器和搜索引擎優化)或更改HREF或LINK的onclick

也別有數字ID

DEMO

<html> 
<head> 
<title>Index</title> 
<script type="test/javascript"> 
var myLinks = [ 
'http://bing.com/search?q=go', 
'http://bing.com/search?q=visit', 
'http://bing.com/search?q=explore', 
'http://bing.com/search?q=funny%20pics' 
]; // note no comma on the last item 
window.onload=function() { 
    for (var i=0;i<myLinks.length;i++) { 
// EITHER document.getElementById("d"+i).getElementsByTagName("a")[0]).href=myLinks[i]; 
// OR 
     document.getElementById("d"+i).getElementsByTagName("a")[0].onclick=(function(idx) { 
     var idx = i; 
     return function() { // closure 
     location=myLinks[idx]; 
     return false; // cancel href 
     } 
    })(i);  
    } 
} 
</script> 
</head> 
<body> 

<div id="d0"><a href="#"">GO</a></div> 
<div id="d1"><a href="#"">VISIT</a></div> 
<div id="d2"><a href="#"">EXPLORE</a></div> 
<div id="d3"><a href="#"">FUNNY PICS</a></div> 

</body> 
</html> 
+0

先生,它不適用於Chrome和Firefox。爲什麼我不知道.. – BPP

+0

離開它PLZ。但我想知道是否有任何腳本將存儲大量的鏈接(即可能是.jpg或.mp3文件),我可以很容易地插入像這樣下載 - '

'就像..? – BPP

+0

謝謝先生,並試圖找出這個腳本「我想知道是否有任何腳本將存儲大量的鏈接(這可能是.jpg或.mp3文件),我可以很容易地插入像這樣下載 - '

'就像..? 「」 – BPP

0

你需要先檢查該元素爲空或不是。

HTML#2

<html> 
<head> 
<title>Index</title> 
</head> 
<body> 

<div id="0002"><a href="#"">GO</a></div> 
<div id="0003"><a href="#"">VISIT</a></div> 
<div id="0004"><a href="#"">EXPLORE</a></div> 
<div id="0005"><a href="#"">FUNNY PICS</a></div> 
<script> 
var element = document.getElementById("0001"); 
if(element!=null) 
{ 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/1.html\''); 
} 
var element = document.getElementById("0002"); 
if(element!=null) 
{ 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/2.html\''); 
} 
var element = document.getElementById("0003"); 
if(element!=null) 
{ 
element.setAttribute('onclick', 'window.location.href=\'http://site.com/3.html\''); 
} 
var element = document.getElementById("0004"); 
if(element!=null) 
{  
element.setAttribute('onclick', 'window.location.href=\'http://site.com/4.html\''); 
} 
var element = document.getElementById("0005"); 
if(element!=null) 
{  
element.setAttribute('onclick', 'window.location.href=\'http://site.com/5.html\''); 
} 
</script> 

</body> 
</html> 
相關問題