ÍøÂçѧԺ

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

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

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

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

JS Switch


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

Conditional statements in JavaScript are used to perform different actions based on different conditions.
¼ÙÉèÓï¾äÔÚJSÖÐÓÃÀ´ÒÀ¾Ý²»Í¬µÄÌõ¼þÖ´Ðв»Í¬µÄÐÐΪ¡£


Examples
Àý×Ó

Switch statement[¿ª¹ØÓï¾ä]
How to write a switch statement.
ÔõÑùдswitch(¿ª¹Ø)Óï¾ä


The JavaScript Switch Statement
JS ¿ª¹ØÓï¾ä

You should use the switch statement if you want to select one of many blocks of code to be executed.
Èç¹ûÏëÔÓÔÚ¼¸¸ö´úÂë¿éÖÐÑ¡ÔñÒ»¸öÀ´ÔËÐоÍʹÓÃswitch(¿ª¹Ø)Óï¾ä

Syntax
Óï·¨

switch(n)
{
case 1:
  execute code block 1
  break    
case 2:
  execute code block 2
  break
default:

  code to be executed if n is
  different from case 1 and 2
}

This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
ËüÊÇÕâÑù¹¤×÷µÄ:Ê×ÏÈ£¬ÓÐΨһµÄÒ»¸ö±í´ïʽ n (´ó¶àÊýΪһ¸ö±äÁ¿)£¬ËüÊDZ»¸³¹ýÖµµÄ¡£ ½ÓÏÂÀ´±í´ïʽ½«Óëÿ¸öcase(ʼþ)½øÐбȽϡ£Èç¹ûÎǺϾÍÖ´ÐиÃʼþÄڵĴúÂë¿é¡£Ê¹ÓÃbreakÀ´·ÀÖ¹´úÂëÖ´Ðкó×Ô¶¯×ªÏòÏÂÒ»¸öʼþ¡£

Example
Àý×Ó

<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
var d=new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
  document.write("Finally Friday")
  break
case 6:
  document.write("Super Saturday")
  break
case 0:
  document.write("Sleepy Sunday")
  break
default:
  document.write("I'm looking forward to this weekend!")
}

</script>

ÆÀÂÛ (13) 1 2 3 All