纯原生js改良版

纯原生js改良版日历,效果如下:


代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body{
font-size: 0px;
}
#c_top_t{
width: 100%;
/*border-collapse: collapse;*/
}
.left div .lefta{
display: inline-block;
width: 0px;
height: 0px;
border-top: 8px solid transparent;
border-right: 8px solid #3081C2;
border-bottom: 8px solid transparent;
border-left: 8px solid transparent;
margin-top: 2px;
margin-left: -2px;
}
.left div .righta{
display: inline-block;
width: 0px;
height: 0px;
border-top: 8px solid transparent;
border-right: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid #3081C2;
margin-top: 2px;
margin-left: 6px;
}
.left{
width: 100%;
position: relative;
text-align: center;
border: 1px solid #B1D2EC;
}
.left div{
display: block;
vertical-align: middle;
height: 20px;
width: 20px;
background-color: #DEECF8;
position: absolute;
text-align: left;
}
input{
display: block;
margin: 0px;
vertical-align: middle;
height: 18px;
border: none;
width: 95%;
padding:1px 1px 1px 5px;
text-align: center;
}
#c_top_t td{
/*border: 1px solid #B1D2EC;*/
position: relative;
}
.rightd{
top: 0px;
right: 0px;
border-left: 1px solid #B1D2EC;
}
.leftd{
border-right: 1px solid #B1D2EC;
}
#calendars{
padding: 0px 10px;
text-align: center;
}
.indictor{
position: absolute;
right: 25px;
top: 5px;
}
.leftshow{
/*height: 200px;*/
left: 0px;
right: -2px;
position: absolute;
background-color: #DEECF8;
display: none;
border: 1px solid #B1D2EC;
border-top:none ;
}
.indictor:hover{
cursor: pointer;
}
.leftshow ul{
padding: 0px;
margin: 0px;
list-style: none;
}
.leftshow ul li{
font-size: 16px;
display: inline-block;
width: 50%;
text-align: center;
margin-top:2px ;
cursor: pointer;
/*color: white;*/
}
.leftshow div{
text-align: center;
/*background-color: darkturquoise;*/
/*height: 16px;*/
}
.leftshow div a.top{
display: inline-block;
margin-top: -4px;
margin-bottom: 4px;
width: 0px;
height: 0px;
border-top: 8px solid transparent;
border-right: 8px solid transparent;
border-bottom: 8px solid #3081C2;
border-left: 8px solid transparent;
}
.leftshow div a.bottom{
display: inline-block;
margin-top: 4px;
margin-bottom: -4px;
width: 0px;
height: 0px;
border-bottom: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid #3081C2;
border-left: 8px solid transparent;
}

.leftshow li:hover{
background-color: white;
}
.top_div{
border-bottom: 1px solid #B1D2EC;
}
.bottom_div{
border-top: 1px solid #B1D2EC;
}
#c_top{
margin-top: -10px;
}
#calendar{
width: 100%;
border-collapse: collapse;
}
#calendar td{
padding: 2px 5px;
font-size: 16px;
border: 1px solid#B1D2EC;
}
</style>
</head>
<body>
<div id="calendars">
<div id="c_top">
<table id="c_top_t" cellpadding="0" border="0" cellspacing="10px">
<tr>
<td><div class="left">
<div class="leftd"><a class="lefta"  href="#" οnclick="yminus()"></a></div>
<input id="year" value="2016年"/><img class="indictor" οnclick="doClicky()" src="img/小箭头.png">
<div class="rightd"><a class="righta" href="#" οnclick="yadd()"></a></div>
</div>
<div class="leftshow" id="leftshow">
<div class="top_div"><a href="#" οnclick="yearminus()" class="top"></a></div>
<ul id="yearul">
</ul>
<div class="bottom_div"><a href="#" οnclick="yearadd()" class="bottom"></a></div>
</div>
</td><td>
<div class="left">
<div class="leftd"><a class="lefta" href="#" οnclick="mminus()"></a></div>
<input  id="month" value="4月"/><img class="indictor" οnclick="doClickm()" src="img/小箭头.png">
<div class="rightd"><a class="righta" href="#" οnclick="madd()"></a></div>
</div>
<div class="leftshow" id="rightshow">
<ul id="monthul">
</ul>
</div>
</td>
</tr>
</table>
<div style="padding: 0px 8px 0px 10px;" >
<table id="calendar" cellpadding="0" border="0" cellspacing="0">
</table>
</div>
</div>
</div>
<script type="text/javascript">
function getStyle(obj,attr)
{
if(obj.currentStyle)
{
return obj.currentStyle[attr];
}
else
{
return document.defaultView.getComputedStyle(obj,false)[attr];
}
}
function doClicky(){
document.getElementById("rightshow").style.display='none';
var leftshow=document.getElementById('leftshow');
var leftshow_d=getStyle(leftshow,"display");
if(leftshow_d=='none')
{
leftshow.style.display='block';
}else{
leftshow.style.display='none';
}
var in_y=document.getElementById("year").value.replace("年",'');
console.log(in_y);
in_y-=5;
var year_ul=document.getElementById('yearul');
removeChildren(year_ul);
for(var i=0;i<10;i++){
var yearli=document.createElement('li');
yearli.innerText=in_y+i+'年';
yearli.οnclick=function(){
document.getElementById("year").value=this.innerText;
createCalendar();
leftshow.style.display='none';
}
year_ul.appendChild(yearli);
}

}
function removeChildren(pnode){    
var childs=pnode.childNodes;    
for(var i=childs.length-1;i>=0;i--){    
pnode.removeChild(childs.item(i));    
}    
}  
function doClickm(){
document.getElementById("leftshow").style.display='none';
var leftshow=document.getElementById('rightshow');
var leftshow_d=getStyle(leftshow,"display");
console.log(leftshow_d);
if(leftshow_d=='none')
{
leftshow.style.display='block';
}else{
leftshow.style.display='none';
}
var year_ul=document.getElementById('monthul');
removeChildren(year_ul);
for(var i=1;i<13;i++){
var yearli=document.createElement('li');
yearli.innerText=i+'月';
yearli.οnclick=function(){
document.getElementById("month").value=this.innerText;
createCalendar();
leftshow.style.display='none';
}
year_ul.appendChild(yearli);
}

}
function yearminus(){
var leftshow=document.getElementById('leftshow');
var in_y=document.getElementById("yearul").childNodes[0].innerText.replace("年",'');
console.log(in_y);
in_y-=10;
var year_ul=document.getElementById('yearul');
removeChildren(year_ul);
for(var i=0;i<10;i++){
var yearli=document.createElement('li');
yearli.innerText=in_y+i+'年';
yearli.οnclick=function(){
document.getElementById("year").value=this.innerText;
leftshow.style.display='none';
createCalendar();
}
year_ul.appendChild(yearli);
}

}
function yadd(){
var yearin=document.getElementById("year");
var year = document.getElementById("year").value.replace("年","");
year = Number(year)+1;
yearin.value=year+"年";
createCalendar();

}
function yminus(){
var yearin=document.getElementById("year");
var year = document.getElementById("year").value.replace("年","");
year = Number(year)-1;
yearin.value=year+"年";
createCalendar();
}
function madd(){
var monthin=document.getElementById("month");
var month=monthin.value.replace("月","");
month=Number(month);
if(month==12)
{
month=1;
}else{
month+=1;
}
monthin.value=month+"月";
createCalendar();

}
function mminus(){
var monthin=document.getElementById("month");
var month=monthin.value.replace("月","");
month=Number(month);
if(month==1)
{
month=12;
}else{
month-=1;
}
monthin.value=month+"月";
createCalendar();

}
function yearadd(){
var leftshow=document.getElementById('leftshow');
var in_y=document.getElementById("yearul").childNodes[0].innerText.replace("年",'');
console.log(in_y);
in_y=Number(in_y);
in_y+=10;
var year_ul=document.getElementById('yearul');
removeChildren(year_ul);
for(var i=0;i<10;i++){
var yearli=document.createElement('li');
yearli.innerText=in_y+i+'年';
yearli.οnclick=function(){
document.getElementById("year").value=this.innerText;
leftshow.style.display='none';
createCalendar();
}
year_ul.appendChild(yearli);
}




}
function createCalendar(){
var year=document.getElementById("year").value.replace("年","");
var month=document.getElementById("month").value.replace("月","");
var calend =document.getElementById('calendar');
removeChildren(calend);
var tr_title=document.createElement('tr');
var titles=['日','一','二','三','四','五','六'];
for(var m=0;m<titles.length;m++){
var td_title=document.createElement('td');
td_title.innerText=titles[m];
tr_title.appendChild(td_title);
}
calend.appendChild(tr_title);
year=Number(year);
month=Number(month)-1;
var nstr=new Date(year,month,1); //当前Date资讯
var firstday=nstr.getDay();//星期几 
var m_days=new Array(31,28+is_leap(year),31,30,31,30,31,31,30,31,30,31); //各月份的总天数
var tr_str=Math.ceil((m_days[month] + firstday)/7); //表格所需要行数
for(i=0;i<tr_str;i++) { //表格的行
  var tr=document.createElement("tr");
            for(k=0;k<7;k++) { //表格每行的单元格
    var td=document.createElement("td");
      idx=i*7+k; //单元格自然序列号
      date_str=idx-firstday+1; //计算日期
      (date_str<=0 || date_str>m_days[month]) ? date_str="" : date_str=idx-firstday+1; //过滤无效日期(小于等于零的、大于月总天数的)
td.innerText=date_str;
// if(date_str!='')
// {
// var f_ds=formatDate(date_str);
// for(var n=0;n<jsonData.data2.length;n++)
//                             {
//                             if(f_ds==jsonData.data2[n])
//                             {
//                             td.className='have';
//                             td.οnclick=function(){
//                                 var f_d=formatDate(this.innerText);
//                                 createDataTable(f_d);
// }
//                             }
//                             }
//
// }
tr.appendChild(td);
   }
calend.appendChild(tr);
}
}
function is_leap(year) { 
   return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));
} //是否为闰年
createCalendar();
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值