<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>早于8:00,会获得你真棒</p>
<button onclick="myfunction()">点这里</button>
<p id="demo"></p>
<script type="text/javascript">
function myfunction(){
var x="";
var time =new Date().getHours();
if(time<8)
{
x="你真棒";
}
document.getElementById('demo').innerHTML=x;
else
{
x="要加油哦";
}
document.getElementById('demo').innerHTML=x;
}
</script>
</body>
</html>
代码结构有问题,删掉圈出来那句
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>早于8:00,会获得你真棒</p>
<button onclick="myfunction()">点这里</button>
<p id="demo"></p>
<script type="text/javascript">
function myfunction(){
var x="";
var time =new Date().getHours();
if(time<8)
{
x="你真棒";
}else{
x="要加油哦";
}
document.getElementById('demo').innerHTML=x;
}
</script>
</body>
</html>
if() 和 else 之间不要有除{}外的其他代码
function myfunction(){
var x="";
var time =new Date().getHours();
if(time<8)
{
x="你真棒";
}
//document.getElementById('demo').innerHTML=x; 这行删除掉
else
{
x="要加油哦";
}
document.getElementById('demo').innerHTML=x;
}
或者两个 document.getElementById('demo').innerHTML=x; 都放到 {}中
function myfunction(){
var x="";
var time =new Date().getHours();
if(time<8)
{
x="你真棒";
document.getElementById('demo').innerHTML=x; //都放到 {}中
}
else
{
x="要加油哦";
document.getElementById('demo').innerHTML=x; //都放到 {}中
}
}
</script>
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
你的if...else结构可以拆开的么?在if和else之间还加了一条语句?如果是这样子,直接把else改成if(time>=8算了)