日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

HTML JS 动态日历表

發布時間:2024/3/26 HTML 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HTML JS 动态日历表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用HTML和JS 創建一個動態日歷表

先看效果圖

Html + JS 代碼

<!DOCTYPE html> <html><head><meta charset="utf-8"><link rel="stylesheet" type="text/css" href="css/Canlendar.css"/><title>動態顯示日歷</title></head><body><table><td style="width:9%" onclick="ShowCalendar(\''20210401'\')"><table style="background-color: #408080;color:#FFFFFF;"><tr><td id="showCalendar" onclick="clickCanlendar()">點擊顯示</td> </tr></table></td></table><script type="text/javascript" src="js/jquery-3.4.1.min.js"></script><script>function clickCanlendar(){var date = new Date();var month = date.getMonth();var day = date.getDate();if(month >= 1 && month <= 9){month = '0' + month}if(day >= 1 && day <= 9){day = '0' + day;};var currentDate = date.getFullYear() + month + day;alert(date);ShowCalendar(currentDate);}function ShowCalendar(SY_OPERATION){//判斷exit 節點是否存在if($("#exit").length > 0){console.log(1);$("#exit").detach();return;}else{console.log(2);var Calendar = '<div id= "exit" class="main clearfix" style="position: absolute;background:white;">' +'<div class="menu clearfix tomove"> '+' <a class="select-prev fl" href="javascript:;">&lt;</a>' + ' <div class="select month fl pr">' + '<div class="select-text"><span class="year-text" value="2016">2016年</span><span class="month-text" value="">1月</span></div>' + ' </div>' + '<a class="select-next fl" href="javascript:;">&gt;</a>' + '<a class="select-today fl" href="javascript:;">返回今天</a>' + '<div class="time fl">10:40:05</div>' + '</div>' + '<ul class="title">' + ' <li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li>' + '</ul>' + '<ul class="table">' + ' <li><span>1</span></li><li><span>2</span>' + ' </ul>' + '</div>';$(Calendar).insertBefore("#showCalendar");console.log(3);$(function(){var init = {// startYear: 1900, //年份列表開始年// endYear: 2050 //年份列表結束年};var fun = {init: function(){this.timeBox();this.dateBox();},timeBox: function(){ //系統時間(function(){var date = new Date();var h = date.getHours(),m = date.getMinutes(),s = date.getSeconds();var time = h + ':' + m + ':' + s;var time = fun.timeBu(time);$('.time').html(time);setTimeout(arguments.callee, 1000);})();},timeBu: function(val){var arr = val.split(':');for(var i = 0; i < arr.length; i++){if(arr[i] < 10){arr[i] = '0' + arr[i];}};return arr.join(':');},showDate: function(year, month){ //日歷展示//改變下拉$('.year-text').text(year + '年').attr('value', year);$('.month-text').text(month + '月').attr('value', month);$('.select-list li').removeClass('selected');//為當前已經默認的年份和有份標為選中$('.select-list li').addClass(function(i){var value = $(this).attr('value');if(value == year || value == month){return 'selected';}});var setDate = new Date();setDate.setFullYear(year); //設置年份setDate.setMonth(month-1); //設置月份,因為系統的月份都是比真實的小1setDate.setDate(1); //設置成當前月第一天var num = setDate.getDay(); //得到本月第一天是星期幾setDate.setMonth(month); //設置成正確的真實月份var lastDate = new Date(setDate.getTime() - 1000*60*60*24); //計算得到當前月最后一天的日期格式var lastDay = lastDate.getDate(); //獲取本月最后一天是幾號//利用得到的當前月總天數來循環出當前月日歷var html = '';for(var i = 1; i <= lastDay; i++){html += '<li><span>'+i+'</span></li>';};$('.table').html(html);var first = $('.table li:first'),w = first.outerWidth();first.css('marginLeft', w * num + 'px'); //根據得到的本月第一天是周幾得出第一個li所在的位置,從而排列好整個日歷var nowDate = new Date(), //得到系統當前的真實時間nowYear = nowDate.getFullYear(),nowMonth = nowDate.getMonth() + 1,today = nowDate.getDate(); //獲取當前是幾號if(nowYear == year && nowMonth == month){ //驗證當前展示中是否包含今天$('.table li').eq(today-1).find('span').addClass('today'); //標出今天}},dateBox: function(){var date = new Date(),year = date.getFullYear(), //獲取當前是哪一年month = date.getMonth() + 1; //獲取當前月//初始化日歷fun.showDate(year, month);}}fun.init(); //運行$(document).on('click', function(){$('.select-list').hide();$('.select-text').removeClass('current');});//切換年,月$('.select-list li').on('click', function(){if($(this).hasClass('selected')){ //如果是已經選中的則不用在重新初始化日歷return;};var self = $(this),text = self.text(),value = self.attr('value');self.addClass('selected').siblings().removeClass('selected');self.parent().next().find('span').text(text).attr('value', value);var year = $('.year-text').attr('value'),month = $('.month-text').attr('value');fun.showDate(year, month);});//返回今天$('.select-today').on('click', function(){fun.dateBox();});//日期選擇//日期懸浮時$('body').on({'mouseenter': function(){$(this).addClass('on');},'mouseleave': function(){$(this).removeClass('on');}}, '.table span');//點擊日期$('body').on('click', '.table span', function(){var year = $('.year-text').attr('value'),month = $('.month-text').attr('value'),day = $(this).text();if(month < 10){month = "0" + month;}if(day < 10){day = "0" + day;}var YYY = "000" + (year-1911).toString();YYY = YYY.substr(3, 3);var datetime = YYY + month + day; //ShowOperation('20210401', datetime);});//前一個月$('.select-prev').on('click', function(){var year = parseInt($('.year-text').attr('value')),month = parseInt($('.month-text').attr('value'));if(month - 1 > 0){month = month - 1;}else{month = 12;year = year - 1;if(year < init.startYear){return;}};fun.showDate(year, month);});//后一個月$('.select-next').on('click', function(){var year = parseInt($('.year-text').attr('value')),month = parseInt($('.month-text').attr('value'));if(month + 1 <= 12){month = month + 1;}else{month = 1;year = year + 1;if(year > init.endYear){return;}};fun.showDate(year, month);})console.log(4);})}}</script></body> </html>

CSS代碼

*{margin:0;padding: 0;font-family: '微軟雅黑';} body{background: #fff;} ul{list-style: none;} a{text-decoration: none;color:#333;} img{border:none;} .fl{float:left;_display:inline;} .fr{float:right;_display:inline;} .pr{position: relative;} .pa{position: absolute;} .none{display: none;} .clearfix:before,.clearfix:after{content:"";display:table;} .clearfix:after{clear:both;} .clearfix{*zoom:1;}.main{width:350px;margin:27px auto;padding:5px;border:2px #d8d8d8 solid;color:#333;} .menu{font-size: 14px;} .select{width:80px;height:25px;line-height:25px;border:0px;cursor: default;} .select-text{text-align: center;} /*.select-text:hover{box-shadow: 1px 1px 0 0 #d8d8d8 inset;}*/ .select div.current{box-shadow: 1px 1px 0 0 #d8d8d8 inset;} .month{border-left:none;border-right:none;} .select-list{left:-1px;top:25px;width: 80px;max-height:360px;background: #fff;border:1px #d8d8d8 solid;overflow: auto;} .select-list li{height: 30px;line-height: 30px;text-indent: 10px;cursor: pointer;} .select-list li.selected{background: #d8d8d8;} .select-list li.on{background: #d8d8d8;} .select-prev,.select-next{width:20px;height: 20px;margin-left:20px;line-height: 25px;text-align: center;cursor: pointer;} .select-next{margin:0;} .select-today{width:80px;height: 25px;margin-left:40px;line-height: 25px;text-align: center;border:1px #d8d8d8 solid;cursor: pointer;} .select-prev:hover,.select-next:hover,.select-today:hover{border:1px #D8D8D8 solid;} .select-prev:active,.select-next:active,.select-today:active{background: #f0f0f0;box-shadow: 1px 1px 1px #c7c7c7 inset;} .time{height: 25px;margin-left:10px;line-height: 25px;} .title{height:35px;font-size: 14px;margin-top:10px;border-top:1px #57abff solid;} .title li{float:left;width:50px;height:35px;line-height: 35px;text-align: center;} .table li{float:left;width:50px;height:40px;font-size: 14px;cursor: pointer;} .table li span{display: block;width:43px;height:35px;line-height: 35px;text-align: center;border:2px #fff solid;} .table li span.on{border:2px #fb0 solid;} .table li span.today{background: #fb0;border:2px #fb0 solid;border-radius:50%;color:#fff;}

好啦!到這里已經成功啦,快去試試吧!???

???????????????????????????????????

更多博客內容請查看 ? 可可西里的博客

如果喜歡的話可以點個贊和關注嗷~ ???

總結

以上是生活随笔為你收集整理的HTML JS 动态日历表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。