當用戶點擊按鈕的日子裏,該腳本會生成一個隨機數,然後顯示相應的工作日。下面的代碼是我到目前爲止,唉,它似乎並沒有工作。 我調試過它,顯然myfunction沒有定義?!使用功能,顯示隨機數,然後ifelse語句來分配一週
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a random number between 1 and 7</p>
<button onclick="myFunction()">Get Random</button>
<p id="task1"></p>
<script>
function myFunction() {
var x = Math.floor((Math.random() * 7 + 1);
document.getElementById("task1").innerHTML = x;
}
if (x===1)
{
document.write("today is sunday");
}
else if(x===2)
{
document.write("today is monday");
}
else if(x===3)
{
document.write("today is tuesday");
}
else if(x===4)
{
document.write("today is wednesday");
}
else if(x===5)
{
document.write("today is thursday");
}
else if(x===6)
{
document.write("today is friday");
}
else if(x===7)
{
document.write("today is saturday");
}
</script>
</body>
</html>
我注意到var x = Math.floor((Math.random()* 7 + 1);中的括號是不平衡的。 – eigenchris