当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js+html制作简单日历的方法

js+html制作简单日历的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

新手一枚,不会写什么高大上的博文,一些平时做的小练习,献丑

代码:

<!doctype html>
<html>

 <head>
  <meta charset="utf-8">
  <title>无标题文档</title>
  <style>
   * {margin: 0;padding: 0}
   #calendar {width: 210px;margin: 100px auto;overflow: hidden;border: 1px solid #000;padding: 20px;position: relative}
   #calendar h4 {text-align: center;margin-bottom: 10px}
   #calendar .a1 {position: absolute;top: 20px;left: 20px;}
   #calendar .a2 {position: absolute;top: 20px;right: 20px;}
   #calendar .week {height: 30px;line-height: 20px;border-bottom: 1px solid #000;margin-bottom: 10px}
   #calendar .week li {float: left;width: 30px;height: 30px;text-align: center;list-style: none;}
   #calendar .datelist {overflow: hidden;clear: both}
   #calendar .datelist li {float: left;width: 30px;height: 30px;text-align: center;line-height: 30px;list-style: none;}
   #calendar .datelist .ccc {color: #ccc;}
   #calendar .datelist .red {background: #f90;color: #fff;}
   #calendar .datelist .sun {color: #f00;}
  </style>
  <script src="js/jquery-1.11.3.min.js"></script>
  <script>
   $(function() {

    //必要的数据
    //今天的年 月 日 ;本月的总天数;本月第一天是周几???
    var inow=0;
    
    function run(n) {

     var odate = new date(); //定义时间
     odate.setmonth(odate.getmonth()+n);//设置月份
     var year = odate.getfullyear(); //年
     var month = odate.getmonth(); //月
     var today = odate.getdate(); //日

     //计算本月有多少天
     var allday = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];

     //判断闰年
     if(month == 1) {
      if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
       allday = 29;
      }
     }

     //判断本月第一天是星期几
     odate.setdate(1); //时间调整到本月第一天
     var week = odate.getday(); //读取本月第一天是星期几

     //console.log(week);
     $(".datelist").empty();//每次清空
     //插入空白

     for(var i = 0; i < week; i++) {
      $(".datelist").append("<li></li>");
     }

     //日期插入到datelist
     for(var i = 1; i <= allday; i++) {
      $(".datelist").append("<li>" + i + "</li>")
     }
     //标记颜色=====================
     $(".datelist li").each(function(i, elm){
      //console.log(index,elm);
      var val = $(this).text();
      //console.log(val);
      if (n==0) {
       if(val<today){
       $(this).addclass('ccc')
      }else if(val==today){
       $(this).addclass('red')
      }else if(i%7==0 || i%7==6 ){
       $(this).addclass('sun')
      }
      }else if(n<0){
       $(this).addclass('ccc')
      }else if(i%7==0 || i%7==6 ){
       $(this).addclass('sun')
      }
     });

     //定义标题日期
     $("#calendar h4").text(year + "年" + (month + 1) + "月");
    };
    run(0);
    
    $(".a1").click(function(){
     inow--;
     run(inow);
    });
    
    $(".a2").click(function(){
     inow++;
     run(inow);
    })
   });
  </script>
 </head>

 <body>
  <div id="calendar">
   <h4>2013年10月</h4>
   <a href="##" rel="external nofollow" rel="external nofollow" class="a1">上月</a>
   <a href="##" rel="external nofollow" rel="external nofollow" class="a2">下月</a>
   <ul class="week">
    <li>日</li>
    <li>一</li>
    <li>二</li>
    <li>三</li>
    <li>四</li>
    <li>五</li>
    <li>六</li>

   </ul>
   <ul class="datelist"></ul>
  </div>

 </body>

</html>

更多精彩内容请点击专题《javascript日历插件使用方法汇总》进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网