如何用js判斷平年瑞年

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        //isNaN()當不是數字 返回true 是數字返回false
        window.onload = function(){
            var btn = document.getElementById('btn');
            btn.onclick = function(){
                var inp = document.getElementById('inp').value;
                // var inp = Number(inp)
                console.log(inp)
                if (isNaN(inp) || inp < 0 || inp == '') {
                    alert('輸入錯誤,或必須大於0,或不能爲空')
                } else if (inp % 4 ==0 && inp % 100 != 0 || inp % 400 == 0) {
                    alert('閏年')
                } else{
                    alert('平年')
                }               
            }
        }
    </script>
</head>
<body>
    <input type="text" id="inp">
    <input type="button" value="確定" id="btn">
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章