页面上输入一个年份(需验证),判断是否是闰年(能被 4 整除,却不能被 100 整除的年份;能被 400 整除的是闰年),并且在页面上显示相应提示信息。
-
-
<html>
-
<head>
-
<title>闰年 </title>
-
<meta charset="utf-8">
-
</head>
-
<body>
-
<form>
-
请输入年份: <input id="year" type="text" />
-
<span id="check"> </span>
-
</form>
-
<script>
-
var input = document.getElementById( "year");
-
var tip = document.getElementById( "check");
-
//输入框失去焦点触发事件
-
input.onblur = function() {
-
var year = input.value.trim();
-
//年份由4位数字组成
-
if( /^\d{4}$/.test(year)) {
-
//能被4整除却不能被100整除的年份;能被400整除的是闰年
-
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
-
tip.innerHTML = "闰年";
-
} else {
-
tip.innerHTML = "非闰年";
-
}
-
} else {
-
tip.innerHTML = "年份格式不正确请重新输入";
-
}
-
}
-
</script>
-
</body>
-
</html>