所以我有一个onclick按钮,它运行3个函数,每个函数都显示一个随机符号。因此,当我按下按钮时,它应该输出3个随机符号,但是当我点击按钮时它只输出一个符号。
<!DOCTYPE html>
<html>
<head>
<title>MyFirstJavaScript</title>
</head>
<body>
<script>
function slots1()
{
var slot1 = Math.floor(Math.random()*4);
if (slot1 == 0) {
document.getElementById('value').innerHTML = "\u2663";
}
if (slot1 == 1) {
document.getElementById('value').innerHTML = "\u2665";
}
if (slot1 == 2) {
document.getElementById('value').innerHTML = "\u2666";
}
if (slot1 == 3) {
document.getElementById('value').innerHTML = "\u2660";
}
}
function slots2()
{
var slot2 = Math.floor(Math.random()*4);
if (slot2 == 0) {
document.getElementById('value').innerHTML = "\u2663";
}
if (slot2 == 1) {
document.getElementById('value').innerHTML = "\u2665";
}
if (slot2 == 2) {
document.getElementById('value').innerHTML = "\u2666";
}
if (slot2 == 3) {
document.getElementById('value').innerHTML = "\u2660";
}
}
function slots3()
{
var slot3 = Math.floor(Math.random()*4);
if (slot3 == 0) {
document.getElementById('value').innerHTML = "\u2663";
}
if (slot3 == 1) {
document.getElementById('value').innerHTML = "\u2665";
}
if (slot3 == 2) {
document.getElementById('value').innerHTML = "\u2666";
}
if (slot3 == 3) {
document.getElementById('value').innerHTML = "\u2660";
}
}
</script>
<button type="button" value="Spin" name="SPIN" onClick="slots1();slots2();slots3();">Spin</button>
<span id="value"></span>
</body>
</html>
答案 0 :(得分:1)
每个函数基本上覆盖了前一个函数所改变的值,因此您看到的唯一值是slots3()
的结果。以下行对于您的所有功能都是通用的,它是您的覆盖:
document.getElementById('value').innerHTML = //some value;