一,效果图。
二,代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript 条件语句</title>
</head>
<body>
<p>如果时间早于20:00,会获得问候“Good day”.</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
var time = new Date().getHours();
if (time < 20) {
x = "Goo day";
}
document.getElementById("demo").innerHTML = x;
}
</script>
<script type="text/javascript">
var d = new Date();
var time = d.getHours();
if (time < 10) {
document.write("<b>早上好</b>")
} else if (time >= 10 && time < 16) {
document.write("<b>hello world!</b>")
} else {
document.write("<b>hello world!</b>")
}
</script>
<p>这个例子演示了if.elseif..else</p>
</body>
</html>
参考资料:《菜鸟教程》