2013-01-29 4 views
0

클릭하면 내 onclick 이벤트가 발사됩니다. 왜 그런지 모르겠다. 아래는 제 코드입니다. 홈 패널이 먼저로드되어야하며 사용자가 aboutButton을 클릭하면 about 패널로 이동해야합니다.onclick 이벤트 발사 클릭하지 않고

JS 빈 : http://jsbin.com/alebox/1/edit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<script> 

function myOnloadFunc() { 

    var homePanel = document.getElementById("homePanel"); 
    var aboutPanel = document.getElementById("aboutPanel"); 
    var settingsPanel = document.getElementById("settingsPanel"); 
    var gamePanel = document.getElementById("gamePanel"); 
    var resultsPanel = document.getElementById("resultsPanel"); 

    // All panels in app 
    var panels = [homePanel, aboutPanel, settingsPanel, gamePanel, resultsPanel]; 


    // Show selected panel and hide all other panels 
    function showPanel(panel) { 
     for (var i = 0; i < panels.length; i++) { 
      if (panels[i] === panel) { 
       // Show panel 
       // this referred to global object, i.e. window 
       panels[i].style.display = "block"; 
      } else { 
       // Hide 
       panels[i].style.display = "none"; 
      } 
     } 
    } 

    showPanel(homePanel); 

    // CODE THAT IS GIVING ME A PROBLEM ///////////////////////// 
    var aboutButton = document.getElementById("aboutButton"); 

    aboutButton.onclick = showPanel(aboutPanel); 
    // CODE THAT IS GIVING ME A PROBLEM ///////////////////////// 
} 

window.onload = myOnloadFunc; 
</script> 

</head> 

<body> 
<!-- homePanel --> 
<div class="panel" id="homePanel"> 
<div align="center"> 
<p><strong>Web App</strong></p> 
<p><a id="playButton">Play</a> &nbsp;&nbsp;&nbsp; <a id="aboutButton">About</a></p> 
</div> 
</div> 

<!-- aboutPanel --> 
<div class="panel" id="aboutPanel"> 
About panel 
</div> 

<!-- settingsPanel --> 
<div class="panel" id="settingsPanel"> 
Settings panel 
</div> 

<!-- gamePanel --> 
<div class="panel" id="gamePanel"> 
Game panel 
</div> 

<!-- resultsPanel --> 
<div class="panel" id="resultsPanel"> 
Results panel 
</div> 
</body> 
</html> 

답변

4

당신이 바로 전화하고 있기 때문에 발포한다! 그래서

당신이 원하는 :

aboutButton.onclick = function(){showPanel(aboutPanel);}; 
2

만 기능에 대한 참조가 제공되어야하는 경우에 기능을 첨부. 당신은 실제로()를 사용할 때 함수를 호출하고 있습니다. 이벤트를 바인딩 할

코드가 있어야한다 :

aboutButton.onclick = showPanel; 
관련 문제