通过年龄判段属相
<html lang="en">
<head>
<meta charset="UTF-8">
<title>swith case---根据年龄判断属相</title>
<style>
#box{
font-size:30px;
}
</style>
</head>
<body>
<h1>请输入您的年龄</h1>
<input type="text" id="age">
<button onclick="getAge()">提交</button>
<br>
<div id="box"></div>
<script>
function getAge(){
//获取input中的值
var age = document.getElementById("age").value;
//进行判断以今年(2017)为基础
switch(age%12){
case 0:
var sx="鸡";
break;
case 1:
var sx="猴";
break;
case 2:
var sx="羊";
break;
case 3:
var sx="马";
break;
case 4:
var sx="蛇";
break;
case 5:
var sx="龙";
break;
case 6:
var sx="兔";
break;
case 7:
var sx="虎";
break;
case 8:
var sx="牛";
break;
case 9:
var sx="鼠";
break;
case 10:
var sx="猪";
break;
case 11:
var sx="狗";
break;
}
document.getElementById("box").innerHTML="您的生肖是: "+sx;
}
// sx生肖可以自定义
/*
0 鸡
1 猴
2 羊
3 马
4 蛇
5 龙
6 兔
7 虎
8 牛
9 鼠
10 猪
11 狗
*/
</script>
</body>
</html>