当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS If...Else
Conditional statements in JavaScript are used to perform different actions based on different conditions.
JS中的条件语句一般用在针对不同的条件来执行不同的动作。
If statement[IF 语句]
How to write an if statement.
书写if语句的方法
If...else statement[IF...else语句]
How to write an if...else statement.
书写if..elseif..else语句的方法
If..else if...else statement[if...elseif...else语句]
How to write an if..else if...else statement.
书写if..else语句的方法
Random link[随机连接]
This example demonstrates a link, when you click on the link it will take you to W3Schools.com OR to RefsnesData.no. There is a 50% chance for each of them.
这个案例举了一个比例链接的案例。当你点击下面的链接时,链接到W3Schools.com或RefsnesData.no的几率各为50%
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
在写代码时经常会遇到想根据不同的判断来执行不同的动作。你可以用假设语句来做到这点。
In JavaScript we have the following conditional statements:
在JS中有以下一些假设(条件)语句:
You should use the if statement if you want to execute some code only if a specified condition is true.
你应该在代码在只有一个状态为真的情况下就执行的时候使用这条语句
if (condition) { code to be executed if condition is true } |
Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!
注意if语句应该用小写,使用大写的话会引起JS错误
<script type="text/javascript"> //Write a "Good morning" greeting if //the time is less than 10 var d=new Date() var time=d.getHours() if (time<10) { document.write("<b>Good morning</b>") } </script> |
<script type="text/javascript"> //Write "Lunch-time!" if the time is 11 var d=new Date() var time=d.getHours() if (time==11) { document.write("<b>Lunch-time!</b>") } </script> |
Note: When comparing variables you must always use two equals signs next to each other (==)!
注意:要比较变量你就必须使用两个等号标记(==)!
Notice that there is no ..else.. in this syntax. You just tell the code to execute some code only if the specified condition is true.
注意这里没有使用else。你只是让代码当条件为真时就执行。
If you want to execute some code if a condition is true and another code if the condition is not true, use the if....else statement.
如果你想条件为真时运行一些代码而不为真时运行另一些代码,就用if...else语句
if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } |
<script type="text/javascript"> //If the time is less than 10, //you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date() var time = d.getHours() if (time < 10) { document.write("Good morning!") } else { document.write("Good day!") } </script> |
You should use the if....else if...else statement if you want to select one of many sets of lines to execute.
如果你想在几种条件中选择一种去执行,那就应该用if....else if...else语句
if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if condition1 and condition2 are not true } |
<script type="text/javascript"> var d = new Date() var time = d.getHours() if (time<10) { document.write("<b>Good morning</b>") } else if (time>10 && time<16) { document.write("<b>Good day</b>") } else { document.write("<b>Hello World!</b>") } </script> |