代码实例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>易优模板</title> </head> <body> <p>点击这个按钮,获得基于时间的问候。</p> <button onclick="myFunction()">点击这里</button> <p id="demo"></p> <script> function myFunction(){ var x=""; var time=new Date().getHours(); if (time<20){ x="Good day"; } else{ x="Good evening"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
尝试一下 »
点击“尝试一下”按钮查看在线实例
请使用 if....else 语句在条件为 true 时执行代码,在条件为 false 时执行其他代码。
语法
if (condition)
{
当条件为 true 时执行的代码
}
else
{
当条件不为 true 时执行的代码
}
{
当条件为 true 时执行的代码
}
else
{
当条件不为 true 时执行的代码
}