**分析:闰年分为普通闰年和世纪闰年
- 普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年,1900年不是闰年);
- 世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年,1900年不是世纪闰年);
<input type="text" id="inp">
<button id="btn">判断</button>
<script>
//判断是否为闰年
//分析:闰年分为普通闰年和世纪闰年
//普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年,1900年不是闰年);
//世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年,1900年不是世纪闰年);
//获取对象函数
function get(id){
return document.getElementById(id);
}
//获取对象
var oInp = get('inp') ;
var oBtn = get('btn') ;
//判断是否为闰年函数
function leapYear(y){
if(y % 4 == 0 && y % 400 != 0 || y % 400 == 0){
return true ;
}
return false ;
}
//绑定点击事件
oBtn.onclick = function(){
//获取年
var y = oInp.value ;
//调用函数判断是否为闰年
var res = leapYear(y) ;
if(res){
alert('是闰年');
}
else{
alert('不是闰年')
}
}
</script>