?????? ???
???? ??? ??? ?? ??? ??? ???? ? ?????.
???
????? ??? ??? ? ?? ?? ??? ?? ?? ??? ???? ???. ???? ???? ???? ? ??? ??? ? ????.
JavaScript??? ??? ?? ???? ??? ? ????.
if ? - ??? ??? true? ???? ??? ????? ? ?? ?????.
if...else ? - ??? true? ?? ?? ??, ??? false? ? ?? ?? ??
if...else if....else ? - ??? ?? ?? ?? ? ??? ????? ? ?? ?????.
switch ? - ? ?? ?????. ??? ?? ?? ?? ? ??? ???? ?
If ?
? ?? ??? ??? true? ???? ??? ?????.
Syntax
if (??)
{
??? true? ? ???? ??
}
??: if? ???? ?????. ???(IF)? ???? JavaScript ??? ?????!
Example
??? 20:00 ??? ?? "Good day"?? ???? ?????:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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="Good day"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
Note, ? ???? ..else? ????.... ??? ??? true? ???? ??? ????? ????? ??????.
????? ???? ??? ???
If...else ?
??? ?? ? ??? ????, ??? ??? ? ?? ??? ????? if....else ?? ?????. .
Syntax
if (??)
{
??? true? ? ???? ??
}
else
{
??? true? ?? ? ???? ??
}
?? ??? 20? ???? "Good day" ???? ????, ??? ??? "Good evening" ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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 if...else ? if....else if...else ?? ???? ?? ?? ? ??? ?????. ??? ??.
Syntax
if (condition1){?? 1? true? ? ???? ??
}
else if (condition2)
{
?? 2? true? ? ???? ??
}
else
{
?? ?? 1? ?? 2? ?? ?? ?? ? ??
}
?? ??? 10:00 ???? "?? ??" ???? ????, ??? 10:00?? ?? "Good morning" ???? ?????. 20:00 ???? "Good day" ???? ????, ??? ??? "Good evening" ???? ?????.
<html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <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>今天好</b>"); } else { document.write("<b>Hello World!</b>"); } </script> <p> 這個例子演示了 if..else if...else 語句。 </p> </body> </html>
????? ???? ??? ???