ÍøÂçѧԺ

w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS If...Else

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

JavaScript
JSÊýѧ¶ÔÏó²Î¿¼
JS×Ö·û´®¶ÔÏó²Î¿¼
JSº¯Êý²Î¿¼
JSʼþ²Î¿¼
Javascript ³£ÓÃÕ..
FFºÍIEϵÄjs¼æÈÝ..
jQuery ¼òµ¥½éÉÜ
jQuery / ºËÐÄ / ..
jQuery / ºËÐÄ / ..
ÈçºÎʹÓÃJSÀ´ÅжÏ..
JavascriptÔÚIEºÍ..
3¸öjs×Ö·û±àÂ뺯Ê..
javascript ÖÐµÄ ..

JS If...Else


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:16121 :: ::

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>

ÆÀÂÛ (3) 1 All