JavaScript 中if条件语句使用范例
下面的代码得到当前的时间的小时数,如果效果20则显示Good day
<!DOCTYPE html> <html> <body> <p>Click the button to get a "Good day" greeting if the time is less than 20:00.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x=""; var time=new Date().getHours(); if (time<20) { x="Good day"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>