"Document is not Defined"

"Document is not Defined"

JavaScript 的新手,我很难解决这个问题。非常感谢

var attempts = 0;
var randomNumber;

while (randomNumber === 10) {
    randomNumber = (Math.random(10 - 1) + 1);
    attempts = attempts + 1;
}

document.write("It took " + attempts + " attempts");

代码应包含在 HTML 片段中,因为您正在调用 document 对象。文档对象代表您的网页。如果您想访问 HTML 页面中的任何元素,您总是从访问文档对象开始。

例如:

<!DOCTYPE html>
<html>
<body>

<script>
var attempts = 0;
var randomNumber;

while (randomNumber === 10) {
    randomNumber = (Math.random(10 - 1) + 1);
    attempts = attempts + 1;
}

document.write("It took " + attempts + " attempts");
</script>

</body>
</html>