基础JS 闰年 数字范围 当月天数
作者:互联网
**判断是否是闰年**
能被4整除不能被100整除
二选一
能被400整除
判断数字的范围
判断当月的天数
注意从页面的输入框获取到的内容时字符数据,根据实际情况选择是否转换成数值
利用了case的穿透简化的重复代码
<input type="text" id="txt"><br>
<input type="button" id="btn1" value="判断平闰年">
<input type="text" id="res1" readonly><br>
<input type="button" id="btn2" value="判断数字的范围">
<input type="text" id="res2" readonly><br>
<input type="button" id="btn3" value="判断当月份天数">
<input type="text" id="res3" readonly><br><br>
var txt = document.getElementById("txt");
var btn1 =document.getElementById("btn1");
var res1 = document.getElementById("res1")
btn1.onclick = function(){
var y = txt.value;
if(y%4===0 && y/100!==0 || y%400===0){
res1.value = y+ "是闰年";
}
else{
res1.value = y + "是平年";
}
}
var btn2 = document.getElementById("btn2");
var res2 = document.getElementById("res2");
btn2.onclick = function(){
var n = Number(txt.value);
if( n > 0){
res2.value = n + "大于0";
}
else if(n === 0){
res2.value = n + "等于0";
}
else if(n < 0){
res2.value = n + "小于0";
}
}
var btn3 = document.getElementById("btn3");
var res3 = document.getElementById("res3");
btn3.onclick = function(){
var m = Number(txt.value)
switch(m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
res3.value = m + "月是31天";
break;
case 2:
res3.value = m + "月是28天";
break;
case 4:
case 6:
case 9:
case 11:
res3.value = m + "月是30天";
break;
default:
res3.value = "输入错误'" + m +"''";
}
}
标签:case,闰年,天数,value,JS,getElementById,res3,var,document 来源: https://blog.csdn.net/qq_26705343/article/details/111241424