Javascript - 未定义的函数错误
问题描述:
我是新来的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>
答
的错误是,你忘了线
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
我花了很多时间看着它,我不相信(但知道它)是如此简单。谢谢 –
答
下面的代码已经纠正你的语法错误,现在的功能被称为(你可以看到所期望的“你好”的警报)。
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>
di
后添加=
看起来你有语法ê你的'aaa'函数中的错误:var di = document.getElementById(“d”+ i)'。它缺少一个等号。 – Nick而我更喜欢数组文字'[0,0,0,0]'而不是构造函数。 –
学习(然后编码)JavaScript的最重要的部分之一是使用浏览器控制台。 ('console.log'和'console.error')在整个代码中,这些行将帮助您进行调试。即使你不使用这些,浏览器在遇到问题时仍会抛出错误和警告,以及(通常不是)发生错误的确切行号。您可以通过大多数现代浏览器中的开发人员工具打开控制台(通常键盘快捷键是“Ctrl + Shift + i”)。学习爱上控制台,欢迎来到SO。 – Dellirium