JavaScript If...Else ?
???
????? ??? ??? ? ??? ??? ?? ?? ??? ??? ???? ???. ???? ???? ???? ? ??? ??? ? ????.
JavaScript??? ??? ?? ???? ??? ? ????.
if ? - ??? ??? true? ?? ??? ????? ? ?? ?????.
if...else ? - ??? true? ? ??? ????, ??? ? ?????. ??? false? ? ?? ??
if...else if....else ? - ??? ?? ?? ?? ? ??? ????? ? ?? ?????.
switch ? - ?? ?? ?? ? ??? ????? ? ?? ?????. ,
if ?? ?????.
??:
if (expr){
?
}
? ??? expr ?? true? ?? {statement}? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var x = 3; var y = 1; if (x>y) alert("x 大于 y"); </script> </head> <body> </body> </html>
if…else
??:
if (expr) {
statement1
} else {
statement2
}
??? expr ????, ???1? ?????. ??? ??? ???2? ??????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var x = 1; var y = 3; if (x>y){ alert("x大于y"); } else { alert("x小于等于y"); } </script> </head> <body> </body> </html>
if...else if...else
??:
if (expr1){
?statement1
} else if (expr2) {
} else {
statement3
}
? ??? expr1? true? ???? statement1? ????, ??? ??? expr2? true? ?? expr2? ????, expr2? true? ???? statement3? ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var x = 3; var y = 3; if (x>y) { alert("x大于y"); } else if (x<y) { alert("x小于y"); } else { alert("x等于y"); } </script> </head> <body> </body> </html>