当用户点击按钮的日子里,该脚本会生成一个随机数,然后显示相应的工作日。下面的代码是我到目前为止,唉,它似乎并没有工作。 我调试过它,显然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