js 萬年歷實現


直接上代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js---萬年歷</title>
    <style type="text/css">
        *{
            margin:0px;
            padding:0px;
            list-style-type:none;
        }
        #box{
            width:450px;
            height:380px;
            border:2px solid #3399ff;
            margin:100px auto 0px;
            border-radius:5px;
        }
        #top{
            width:100%;
            height:50px;
            border-bottom:1px solid #3399ff;
            font-size:15px;
        }
        #year{
            margin:15px 10px;

        }
        #year,#month{
            text-align:center;
            margin-right:15px;
        }
        #con{
            width:420px;
            height:300px;
            margin:0px auto 0px;
        }
        #con ul.week{
            width:420px;
            height:45px;
        }
        #con ul.week li{
            width:60px;
            height:45px;
            line-height:45px;
            text-align:center;
            float:left;
        }
        #con ul.week li.weekend{color:red}
        #con ul.day li{
            width:60px;
            height:45px;
            border-top:1px solid #ddd;
            float:left;
            line-height:50px;
            text-align:center;
            font-size:20px;
        }
    </style>
</head>
<body>
    <div id="box">
        <div id="top">
            <select name="query-year" id="year">
                <option value="">Select year</option>
            </select>年
            <select name="query-month" id="month">
                <option value="">Select month</option>
            </select>月
            <button style="width:60px;margin-left:20px;" id="inquriy" onclick="query()">query</button>
        </div>
        <div id="con">
            <ul class="week">
                <li class="weekend">日</li>
                <li>一</li>
                <li>二</i>
                <li>三</li>
                <li>四</li>
                <li>五</li>
                <li class="weekend">六</li>
            </ul>
            <ul class="day">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>
</body>
<script type="text/javascript">
    /* 
       1 自動生成年份和月份;
       2 默認當前年月;
    */
    var oYear = document.getElementById('year');
    var oMonth = document.getElementById('month');
    var oday = document.querySelector('.day');
    var oLi = document.querySelectorAll('.day li');
    var curDate = new Date();
    // 年份
    eachFor(1900,2051,function(val){
        var option = document.createElement("option");
        option.innerHTML = val;
        option.value = val;
        if(curDate.getFullYear() == val){
            option.selected = true;
        }
        oYear.appendChild(option);
    });
    // 月份
    eachFor(0,11,function(val){
        var option = document.createElement("option");
        option.innerHTML = parseInt(val) +1;
        option.value = val;
        if(curDate.getMonth() == val){
            option.selected = true;
        }
        oMonth.appendChild(option);
    });

    // 對函數的封裝應該考慮摻入的參數,大小類型等
    function eachFor(start, end, callback){
        for(var i = start; i <= end ; i++){
            callback(i);
        }
    }
    function query(){
        var year = oYear.value ;
        var month = oMonth.value ; 
        // console.log(year +"--"+month);
        // oday.innerHTML = '';
        if(year && month){
            console.log(year +"--"+month);
            // 獲取月份的第一天
            var firstDate = new Date(year,month,1);
            // 第一天對應是周幾
            var dayOfWeek = firstDate.getDay();
            // 該月份有多少天
            var conutDay =  getMonthDays(year,month);
            console.log("countDay="+conutDay);
            oLi.forEach(function(el,index){
                el.innerHTML = '';
            })
            eachFor(1,conutDay,function(val){
                oLi[dayOfWeek + val - 1].innerHTML = val;
            });
        }else{
            alert('請先選擇年份和月份');
        }
    }
    // 是否為閏年 整百年份必須被400整除 非整百年份被4整除就都是閏年
    function getMonthDays(year,month){
        var datas = [];
        if(year%400 ==0 || (year%4==0 && year%100!=0)){
            // console.log(year + '是閏年');
            datas = [31,29,31,30,31,30,31,31,30,31,30.31];
        
        }else{
            // console.log(year + '不是閏年');
            datas = [31,28,31,30,31,30,31,31,30,31,30.31];
        }
        console.log(year +"--"+ month +"--"+ datas[month]);
        return datas[month];
    }
    // 初始默認當前時間的年和月進行搜索
    query();
</script>
</html>

只是簡單實現,還有很多需要完善的地方;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM