我想要做的就是讓用戶單擊一個按鈕來生成一個隨機數。它似乎工作,但號碼只在消失前顯示一秒。 這是我的代碼。在JavaScript中生成一個隨機數
<!DOCTYPE html>
<head>
</head>
<body>
<h3>
This is a random number generator!
</h3>
<form onsubmit= "randomNumber()">
<input type="submit" value="Click me!">
</form>
<script>
function randomNumber() {
document.write(Math.floor(Math.random() * 10));
}
</script>
<p id="number">
</p>
</body>
</html>
您需要阻止表單提交,並且[您不應該使用'document.write'](http://stackoverflow.com/q/802854/1048572),因爲它會清除當前頁面。 – Bergi
不要使用document.write http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – epascarello
@epascarello:1s太晚了:-D – Bergi