用javascript实现节假日自动切换风格

1.设置函数

function set(){
    var date_time = new Date();
    var month = date_time.getMonth()+1;
    var day = date_time.getDate();
    var doc_head = document.head;
    var link_list = document.getElementsByTagName("link");
    var link_style = document.createElement("link");
    if( month===10 && day===1){
        var red = document.createElement("link");
        red.setAttribute("rel","stylesheet");
        red.setAttribute("href","../css/red.css");
        link_style.setAttribute("rel","stylesheet");
        link_style.setAttribute("href","../css/GQ.css");
        doc_head.appendChild(red);
        doc_head.appendChild(link_style);

    }else if(month===1 && day===1){
                if( link_list){
                    for(i=0;i<link_list.length;i++){
                        if(link_list[i].getAttribute("ty")==="blue"){
                            doc_head.removeChild(link[i]);
                        }
                    }
                }
        var red = document.createElement("link");
        red.setAttribute("rel","stylesheet");
        red.setAttribute("href","../css/red.css");
        link_style.setAttribute("rel","stylesheet");
        link_style.setAttribute("href","../css/YD.css");
        doc_head.appendChild(red);
        doc_head.appendChild(link_style);
    }else if(month===2 && day===4){
        var red = document.createElement("link");
        link_style.setAttribute("rel","stylesheet");
        link_style.setAttribute("href","../css/red.css");
        doc_head.appendChild(link_style);
    }else if(month===5 && day===1){
        link_style.setAttribute("rel","stylesheet");
        link_style.setAttribute("href","../css/blue.css");
        doc_head.appendChild(link_style);
    }
}

  

2.页面打开始调用函数

  window.onload=function(){
            set();
        }

  

posted @ 2019-01-25 18:18  WH~Z  阅读(460)  评论(0编辑  收藏  举报