2012-03-31 47 views
1

所以,正如你所看到的我可以使用javascript來顯示/隱藏元素,但是當你點擊.png時,它會顯示文本,但不會隱藏其他元素的文本。我一直在翻閱這個腳本,谷歌搜索它,我不能拿出答案。我想我需要另一雙眼睛,所以如果有人看看這個,讓我知道是否有錯誤或者是否遺漏了一些代碼。Javascript顯示/隱藏元素,爲什麼這段代碼不工作?

<html> 
<head> 
<meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no" /> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<script type="text/javascript" src="NKit.js"></script> 

<link rel="stylesheet" type="text/css" href="style.css" /> 

<script type="text/javascript"> 

function showStuff(id) { 
    document.getElementById(id).style.display = 'block'; 
} 
function hideStuff(id) { 
    document.getElementById(id).style.display = 'none'; 
} 

</script> 

</head> 

<body> 

<div class="content"> 

<section class="left"> 

     <p> 
      <a href="#" onclick="showStuff('character1');" onclick="hideStuff('character2');"><img src="character1.png" id="char1" /></a> 

     </p> 

     <p> 
      <a href="#" onclick="showStuff('character2');" onclick="hideStuff('character1');"><img src="character2.png" id="char2" /></a> 
     </p> 

</section> 

<section class="right"> 

     <span id="character1" style="display: none;">Show character1 information</span> 

     <span id="character2" style="display: none;">Character 2 information</span> 

</section> 

</div> 

</body> 
</html> 
+1

爲什麼在每個錨中有兩次「onClick」? – 2012-03-31 21:36:37

回答

1
<a href="#" onclick="showStuff('character1');" onclick="hideStuff('character2');"> 

在這一行,你先物業onclick設置爲showStuff('character1'),那麼你重新分配hideStuff('character2')

因此,您應該將這兩個函數調用包裝爲一個函數,然後將該函數分配給onclick

onclick="function() { showStuff('character1'); hideStuff('character2'); } 
+0

我刪除了'onclick ='周圍的空格... – 2012-03-31 21:44:29