我在chrome调试器中收到myFunction未定义的通知。可以大大使用一些帮助。谢谢。
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of as long as i is less than 10.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var text = "";
var i;
if (math.random > .5) i === "Heads"}
else { i === "tails"
};
while (i < 100) {
text += "<br>This Is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
答案 0 :(得分:1)
您还应该使用您提供的代码在控制台中看到另一个错误:
Uncaught SyntaxError: Unexpected token else
if (math.random > .5) i === "Heads"}
else { i === "tails"
};
应该是:
if (Math.random() > .5) {
i = "Heads";
}
else {
i = "tails";
};
答案 1 :(得分:-2)
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
var text = "";
var i;
if (math.random > .5) i === "Heads"}
else { i === "tails"
};
while (i < 100) {
text += "<br>This Is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
}
</script>
<p>Click the button to loop through a block of as long as i is less than 10.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>