我有一个项目,要求将一系列随机值映射到按钮标题,并且在随机生成后分配标题时遇到问题。以下是我的HTML和JavaScript:
HTML:
data
JavaScript:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="javaFile.js"></script>
</head>
<body>
<h1>Matching Game</h1>
<hr>
<br>
<button class="gameBtn" id="b00"></button>
<table>
<tr>
<td></td>
<td><button class="gameBtn" id="b01">NO SHOW</button></td>
<td><button class="gameBtn" id="b02">NO SHOW</button></td>
<td><button class="gameBtn" id="b03">NO SHOW</button></td>
</tr>
<tr>
<td><button class="gameBtn" id="b10">NO SHOW</button></td>
<td><button class="gameBtn" id="b11">NO SHOW</button></td>
<td><button class="gameBtn" id="b12">NO SHOW</button></td>
<td><button class="gameBtn" id="b13">NO SHOW</button></td>
</tr>
<tr>
<td><button class="gameBtn" id="b20">NO SHOW</button></td>
<td><button class="gameBtn" id="b21">NO SHOW</button></td>
<td><button class="gameBtn" id="b22">NO SHOW</button></td>
<td><button class="gameBtn" id="b23">NO SHOW</button></td>
</tr>
<tr>
<td><button class="gameBtn" id="b30">NO SHOW</button></td>
<td><button class="gameBtn" id="b31">NO SHOW</button></td>
<td><button class="gameBtn" id="b32">NO SHOW</button></td>
<td><button class="gameBtn" id="b33">NO SHOW</button></td>
</tr>
</table>
</body>
</html>
因此,在上面,我对大多数按钮分配进行了注释,以专注于正确设置第一部分。随机生成有效,因为当我显示警告消息时,它会正确显示随机数组。但是,当我包含按钮分配时,它不会处理警报(我相信这是因为代码引发错误,因此并没有达到这一点。
同样在上面,我拉出按钮以查看表是否是导致问题的原因,但是无论哪种方式都没有作用。应该在表的第一部分。
我检查了其他答案,并按照相同的格式标记了按钮,然后再次检查了文件名以确保一切正确。这里只是一点点迷路。任何帮助表示赞赏。
答案 0 :(得分:0)
也许是这样的:
var startingList = ["1", "1", "2", "2", "3", "3", "4", "4", "5", "5", "6", "6", "7", "7", "8", "8"];
var gameList = [];
function int_rnd(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function onLoad() {
for (var key in startingList) {
var rndNum = int_rnd(key, startingList.length - 1);
gameList.push(startingList[rndNum]);
startingList.splice(rndNum, 1);
}
document.getElementById("b00").innerHTML = gameList[0];
}
window.onload = onLoad;
<h1>Matching Game</h1>
<hr>
<br>
<button class="gameBtn" id="b00"></button>
<table>
<tr>
<td></td>
<td><button class="gameBtn" id="b01">NO SHOW</button></td>
<td><button class="gameBtn" id="b02">NO SHOW</button></td>
<td><button class="gameBtn" id="b03">NO SHOW</button></td>
</tr>
<tr>
<td><button class="gameBtn" id="b10">NO SHOW</button></td>
<td><button class="gameBtn" id="b11">NO SHOW</button></td>
<td><button class="gameBtn" id="b12">NO SHOW</button></td>
<td><button class="gameBtn" id="b13">NO SHOW</button></td>
</tr>
<tr>
<td><button class="gameBtn" id="b20">NO SHOW</button></td>
<td><button class="gameBtn" id="b21">NO SHOW</button></td>
<td><button class="gameBtn" id="b22">NO SHOW</button></td>
<td><button class="gameBtn" id="b23">NO SHOW</button></td>
</tr>
<tr>
<td><button class="gameBtn" id="b30">NO SHOW</button></td>
<td><button class="gameBtn" id="b31">NO SHOW</button></td>
<td><button class="gameBtn" id="b32">NO SHOW</button></td>
<td><button class="gameBtn" id="b33">NO SHOW</button></td>
</tr>
</table>