现在的位置: 首页 > 综合 > 正文

JS If…Else

2013年09月11日 ⁄ 综合 ⁄ 共 3326字 ⁄ 字号 评论关闭

Conditional statements in JavaScript are used to perform different actions based on different conditions.
JS中的条件语句一般用在针对不同的条件来执行不同的动作。


Examples
例子

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%


Conditional Statements
假设语句

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中有以下一些假设(条件)语句:

  • if statement - use this statement if you want to execute some code only if a specified condition is true
    这条语句一般是在代码在只有一个状态为真的情况下就执行的时候使用
  • if...else statement - use this statement if you want to execute some code if the condition is true and another code if the condition is false
    两个状态,一种为真,还有种不为真,分别执行不同动作。
  • if...else if....else statement - use this statement if you want to select one of many blocks of code to be executed
    你想在多个条件中选择一个或几个去执行,就用这个
  • switch statement - use this statement if you want to select one of many blocks of code to be executed
    在许多条件中选择一个去执行,用这个

If Statement
if语句

You should use the if statement if you want to execute some code only if a specified condition is true.
你应该在代码在只有一个状态为真的情况下就执行的时候使用这条语句

Syntax
语法

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错误

Example 1
例子1

<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>

Example 2
例子2

<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...else Statement
If...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语句

Syntax
语法

if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true

}

Example
例子

<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>


If...else if...else Statement
If...else if...else 语法

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语句

Syntax
语法

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
}

Example
例子

<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>

 

抱歉!评论已关闭.