JavaScript Tutorial/Statement/Switch

Материал из Web эксперт
Версия от 11:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Switch Statement with calculation

   <source lang="javascript">

<html> <head> <title>Switch Statement Demo</title> <script language="javascript" type="text/javascript">

</script> </head> <body>

Switch Statement Demo

</body> </html></source>


Switch statement with default value

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Switch Statement with Integer as the control variable

   <source lang="javascript">

<html> <head> <title>Switch Statement Demo</title> <script language="javascript" type="text/javascript">

</script> </head> <body>

Switch Statement Demo

</body> </html></source>


Switch statement with return value from a prompt dialog

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Switch with boolean value

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


Use switch statement to deal with form input value

   <source lang="javascript">

<html>

   <head>
     <title>Using the switch statement</title>
   <script language="JavaScript">
   
   </script>
   </head>
   <body>
   <form name="myForm">
   Please enter a day of the week:
<input type=TEXT value="" name="day"> <input type=BUTTON value="Verify" name="myButton" onClick="verifyDay(this.form)"> </form> </body> </html></source>

Using the switch Structure

JavaScript offers the switch statement as an alternative to using the if...else structure.

The switch statement is useful when testing all the possible results of an expression.

The format of a switch structure looks like the following:



   <source lang="javascript">

switch (expression)

   {
     case label1:
       statement1;
       break;
     case label2:
       statement2;
       break;
     default:
       statement3;
   }</source>