2017-10-28 92 views
0

我是新來的html和java腳本。對於我的代碼,我一直得到我的功能沒有定義,我不知道爲什麼。我嘗試將腳本移動到head標籤並在通話之後。我嘗試改變函數的名字,以確保它沒有被使用。我想我缺少一些小而愚蠢的東西。任何幫助將不勝感激Javascript - 未定義的函數錯誤

<html> 

    <script type= "text/javascript" > 

    var d = new Array(0,0,0,0,0); 

    function aaa() 
    { alert("hello"); 
     for (var i = 0; i<5; i++){ 
     var da = Math.floor(Math.random()*6) +1; 
     var di document.getElementById("d" + i); 
     di.src = "pix/dice" + d + ".gif"; 
     } 
    } 

    function newGame(){ 
    } 

    function click1() { 
     var total = 0; 
     for (var i = 0; i <5; i++){ 
     if (d[i] == 1) total +=1;} 
     var loco = document.getElementById("s1"); 
     loco.innerHTML = total; 

     } 

    </script> 

    <h1>Yahtzee</h1> 

    <div style = "text-align:center;"> 
     <img src = "pix/dice0.gif" id = "d0"> 
     <img src = "pix/dice0.gif" id = "d1"> 
     <img src = "pix/dice0.gif" id = "d2"> 
     <img src = "pix/dice0.gif" id = "d3"> 
     <img src = "pix/dice0.gif" id = "d4"> 
    </div> 
    <table border =2 align =center> 
     <tr> 
      <td class = "c1"> 1's </td> 
      <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> </td> 
      <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa()";>Roll</button> </td> 
     </tr> 
     <tr> 
     <td class = "c1"> 2's </td> 
      <td class = "c2"><div class = "scr" id = "s2"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 3's </td> 
      <td class = "c2"><div class = "scr" id = "s3"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 4's </td> 
      <td class = "c2"><div class = "scr" id = "s4"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 5's </td> 
      <td class = "c2"><div class = "scr" id = "s5"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 6's </td> 
      <td class = "c2"><div class = "scr" id = "s6"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> 3 of a kind </td> 
      <td class = "c2"><div class = "scr" id = "k3"> </div></td> 
      <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> 
     </tr> 
     <tr> 
      <td class = "c1"> 4 of a kind </td> 
      <td class = "c2"><div class = "scr" id = "k4"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Full House </td> 
      <td class = "c2"><div class = "scr" id = "fh"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Sm Straight </td> 
      <td class = "c2"><div class = "scr" id = "ss"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Lg Straight </td> 
      <td class = "c2"><div class = "scr" id = "ls"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Yahtzee </td> 
      <td class = "c2"><div class = "scr" id = "yah"> </div></td>  
     </tr> 
     <tr> 
      <td class = "c1"> Chance </td> 
      <td class = "c2"><div class = "scr" id = "cha"> </div></td> 
     </tr> 
     <tr> 
      <td class = "c1"> Total</td> 
      <td class = "c2"><div class = "scr" id = "tot"> </div></td> 
     </tr> 

    </table> 

</body> 

+2

di後添加=看起來你有語法ê你的'aaa'函數中的錯誤:var di = document.getElementById(「d」+ i)'。它缺少一個等號。 – Nick

+0

而我更喜歡數組文字'[0,0,0,0]'而不是構造函數。 –

+0

學習(然後編碼)JavaScript的最重要的部分之一是使用瀏覽器控制檯。 ('console.log'和'console.error')在整個代碼中,這些行將幫助您進行調試。即使你不使用這些,瀏覽器在遇到問題時仍會拋出錯誤和警告,以及(通常不是)發生錯誤的確切行號。您可以通過大多數現代瀏覽器中的開發人員工具打開控制檯(通常鍵盤快捷鍵是「Ctrl + Shift + i」)。學習愛上控制檯,歡迎來到SO。 – Dellirium

回答

0

的錯誤是,你忘了線

var di document.getElementById("d" + i); 

var d = new Array(0,0,0,0,0); 
 
\t \t 
 
\t \t function aaa() 
 
\t \t { \t 
 
     var i = 0 
 
     alert("hello"); 
 
\t \t \t for(i; i<5; i++){ 
 
     var randomn = Math.random(); 
 
\t \t \t var da = Math.floor(randomn*6) +1; 
 
     var elid = "d" + i; 
 
\t \t \t var di = document.getElementById(elid); 
 
\t \t \t di.src = "pix/dice" + d + ".gif"; 
 
\t \t \t } 
 
\t \t } 
 
\t \t 
 
\t \t function newGame(){ 
 
\t \t } 
 
\t \t 
 
\t \t function click1() \t { 
 
\t \t \t var total = 0; 
 
\t \t \t for (var i = 0; i <5; i++){ 
 
\t \t \t if (d[i] == 1){total +=1;} 
 
\t \t \t var loco = document.getElementById("s1"); 
 
\t \t \t loco.innerHTML = total; 
 
\t \t \t } 
 
\t \t \t }
<h1>Yahtzee</h1> 
 
\t \t 
 
\t \t <div style = "text-align:center;"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d0"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d1"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d2"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d3"> 
 
\t \t \t <img src = "pix/dice0.gif" id = "d4"> 
 
\t \t </div> 
 
\t \t <table border =2 align =center> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 1's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> \t </td> 
 
\t \t \t \t <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa();">Roll</button> </td> 
 
\t \t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t <td class = "c1"> 2's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s2"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 3's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s3"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 4's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s4"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 5's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s5"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 6's </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "s6"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 3 of a kind </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "k3"> </div> \t </td> 
 
\t \t \t \t <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> 4 of a kind </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "k4"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Full House </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "fh"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Sm Straight </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "ss"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Lg Straight </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "ls"> </div></td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Yahtzee </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "yah"> </div> \t </td> 
 
\t \t \t \t 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Chance </td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "cha"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td class = "c1"> Total</td> 
 
\t \t \t \t <td class = "c2"><div class = "scr" id = "tot"> </div> \t </td> 
 
\t \t \t </tr> 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t </table>

+0

我花了很多時間看着它,我不相信(但知道它)是如此簡單。謝謝 –

0

下面的代碼已經糾正你的語法錯誤,現在的功能被稱爲(你可以看到所期望的「你好」的警報)。

var d = [0,0,0,0,0]; 
 

 
    function aaa() 
 
    { alert("hello"); 
 
     for (var i = 0; i<5; i++){ 
 
     var da = Math.floor(Math.random()*6) +1; 
 
     var di = document.getElementById("d" + i); 
 
     di.src = "pix/dice" + d + ".gif"; 
 
     } 
 
    } 
 

 
    function newGame(){ 
 
    } 
 

 
    function click1() { 
 
     var total = 0; 
 
     for (var i = 0; i <5; i++){ 
 
     if (d[i] == 1) total +=1;} 
 
     var loco = document.getElementById("s1"); 
 
     loco.innerHTML = total; 
 

 
     }
<html> 
 

 
    <body> 
 
    
 
    <h1>Yahtzee</h1> 
 

 
    <div style = "text-align:center;"> 
 
     <img src = "pix/dice0.gif" id = "d0"> 
 
     <img src = "pix/dice0.gif" id = "d1"> 
 
     <img src = "pix/dice0.gif" id = "d2"> 
 
     <img src = "pix/dice0.gif" id = "d3"> 
 
     <img src = "pix/dice0.gif" id = "d4"> 
 
    </div> 
 
    <table border =2 align =center> 
 
     <tr> 
 
      <td class = "c1"> 1's </td> 
 
      <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> </td> 
 
      <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa()";>Roll</button> </td> 
 
     </tr> 
 
     <tr> 
 
     <td class = "c1"> 2's </td> 
 
      <td class = "c2"><div class = "scr" id = "s2"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 3's </td> 
 
      <td class = "c2"><div class = "scr" id = "s3"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 4's </td> 
 
      <td class = "c2"><div class = "scr" id = "s4"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 5's </td> 
 
      <td class = "c2"><div class = "scr" id = "s5"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 6's </td> 
 
      <td class = "c2"><div class = "scr" id = "s6"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 3 of a kind </td> 
 
      <td class = "c2"><div class = "scr" id = "k3"> </div></td> 
 
      <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> 4 of a kind </td> 
 
      <td class = "c2"><div class = "scr" id = "k4"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Full House </td> 
 
      <td class = "c2"><div class = "scr" id = "fh"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Sm Straight </td> 
 
      <td class = "c2"><div class = "scr" id = "ss"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Lg Straight </td> 
 
      <td class = "c2"><div class = "scr" id = "ls"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Yahtzee </td> 
 
      <td class = "c2"><div class = "scr" id = "yah"> </div></td>  
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Chance </td> 
 
      <td class = "c2"><div class = "scr" id = "cha"> </div></td> 
 
     </tr> 
 
     <tr> 
 
      <td class = "c1"> Total</td> 
 
      <td class = "c2"><div class = "scr" id = "tot"> </div></td> 
 
     </tr> 
 

 
    </table> 
 

 
</body>