JavaScript中的switch case使用方法和Java是一样的,只不过,JavaScript中支持所有类型,而Java只支持整形,char,string。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function f1(){
var val = document.getElementById("val").value;
//alert(val);
switch (val) {
case 11:
alert("a");
break;
case 22:
alert("b");
break;
case "aa":
alert("c");
break;
case "bb":
alert("d");
break;
default:
alert("e");
break;
}
}
</script>
<input type="text" id="val" >
<input type="button" value="赋值" οnclick="f1()">
</body>
</html>