当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS Switch

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript 中的 JS Switch


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1198 ::
收藏到网摘: n/a

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 (大多数为一个变量),它是被赋过值的。 接下来表达式将与每个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>

评论 (0) All

登陆 | 还没注册?