JavaScript中if else条件语句使用范例
下面的代码判断当前时间的小时数是否超过20点,如果小于则显示Good day否则显示Good evening
<!DOCTYPE html> <html> <body> <p>Click the button to get a time-based greeting.</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"; } else { x="Good evening"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>