效果:
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>月的天数</title>
</head>
<body>
<script type="text/javascript">
var flag;
function estimate(){
flag = 0;
var year = parseInt(document.getElementById("t1").value);
flag = year%4 == 0 && year%100 != 0 || year%400==0;
if(flag)
document.getElementById("t1").value = year + '是闰年';
else
document.getElementById("t1").value = year + "是平年";
}
function cal_days(){
var month = parseInt(document.getElementById("t2").value);
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
document.getElementById("t2").value = month + "有31天";
break;
case 4: case 6: case 9: case 11:
document.getElementById("t2").value = month + "有30天";
break;
case 2:
if(flag)
document.getElementById("t2").value = month + "有29天";
else
document.getElementById("t2").value = month + "有28天";
break;
}
}
</script>
<div id="b-box">
<div>
请输入年份:
<input type="text" name="" id="t1" value="" />
</div>
<div>
请输入月份:
<input type="text" name="" id="t2" value="" />
</div>
<div>
<input type="button" name="" id="" value="计算" onclick="estimate(); cal_days();"/>
</div>
</div>
</body>
</html>