我是新來的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>
di
後添加=
看起來你有語法ê你的'aaa'函數中的錯誤:var di = document.getElementById(「d」+ i)'。它缺少一個等號。 – Nick而我更喜歡數組文字'[0,0,0,0]'而不是構造函數。 –
學習(然後編碼)JavaScript的最重要的部分之一是使用瀏覽器控制檯。 ('console.log'和'console.error')在整個代碼中,這些行將幫助您進行調試。即使你不使用這些,瀏覽器在遇到問題時仍會拋出錯誤和警告,以及(通常不是)發生錯誤的確切行號。您可以通過大多數現代瀏覽器中的開發人員工具打開控制檯(通常鍵盤快捷鍵是「Ctrl + Shift + i」)。學習愛上控制檯,歡迎來到SO。 – Dellirium