源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript写的日历类(基于pj)

  • 时间:2020-11-17 22:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript写的日历类(基于pj)
先看看效果:
[url=javascript:void(0)] this.spans=pj("span",this.container.get()); this.as=pj("a",this.container.get()).setStyle({textDecoration:"none",color:"#333"}); this.selects=pj("select",this.container.get()); this.getCurrentDate=function(){return currentDate;}; this.init(); this.initYearList(yearListLength||70); var change=function(){},select=function(){}; this.onChange=function(fn){//当改变年份或者月份是的事件监听器,以当前Calendar对象为上下文执行fn函数 if(pj.isFunction(fn))change=fn; }; this.onSelect=function(fn){//当选择日期时触发的函数,以当前Calendar对象为上下文执行fn函数 if(pj.isFunction(fn))select=fn; }; this.selects[0].onchange=function(){ currentDate.setFullYear(parseInt(this.options[this.selectedIndex].value)); _this.init(); change.apply(_this,arguments); };//选择年份 this.selects[1].onchange=function(){ currentDate.setMonth(parseInt(this.options[this.selectedIndex].value)-1); _this.init(); change.apply(_this,arguments); };//选择月份 this.selects[1].options[currentDate.getMonth()].selected=true; this.as.addListener({ click:function(){currentDate.setDate(parseInt(this.innerHTML));select.apply(_this,arguments);}, mouseover:function(){if(_this.todate.getDate()+""!=this.innerHTML)this.style.color="#CCC";}, mouseout:function(){if(_this.todate.getDate()+""!=this.innerHTML)this.style.color="#333";} }); pj("td",this.container.get(0)).addListener({ mouseover:function(){this.style.backgroundColor="#303";}, mouseout:function(){this.style.backgroundColor="#9CF";} }); } Calendar.prototype={ init:function(){ var cur=this.getCurrentDate(), i=new Date(cur.getFullYear(),cur.getMonth(),1).getDay(),//取星期 j=new Date(cur.getFullYear(),cur.getMonth()+1,0).getDate();//取当月最大日数 //alert(i); this.spans[0].innerHTML=cur.getFullYear(); this.spans[1].innerHTML=cur.getMonth()+1; var m=0,n=this.as.length-1,isTodate=Calendar.isThisMonth(cur); while(m<n){this.as[m].innerHTML=this.as[n].innerHTML='';n--;m++;}//清空 for(var day=1;day<=j;day++,i++){ this.as[i].innerHTML=day; if(isTodate&&day==this.todate.getDate()){ this.todateLink=this.as[i]; pj.setStyle(this.as[i],{color:"#F60",fontWeight:"bold"}); }else if(!isTodate&&day==this.todate.getDate()&&this.todateLink){ pj.setStyle(this.todateLink,{color:"#333",fontWeight:"normal"}); } } }, initYearList:function(len){ Calendar.emptySelect(this.selects[0]); var cur=this.getCurrentDate(),now=this.todate.getFullYear(),max=Math.max(now-cur.getFullYear(),len); for(var y=0;y<=max;y++){//年份下拉列表长度为12 var option=document.createElement("option"); if(cur.getFullYear()==now)option.selected=true; else option.selected=false; option.text=now; option.value=now--; try{ this.selects[0].add(option,null); }catch(e){ this.selects[0].add(option); } } }, getDateString:function(format){//format是日期格式,如yyyy-mm-dd if(!format||!/y{4}.m{2}.d{2}/.test(format))format="yyyy-mm-dd"; format=format.replace(/^yyyy/,this.getCurrentDate().getFullYear()); format=format.replace(/mm/,Calendar.fx(this.getCurrentDate().getMonth()+1)); format=format.replace(/dd/,Calendar.fx(this.getCurrentDate().getDate())); return format; }, todate:new Date(), todateLink:null, show:function(duration){this.container.show(duration);}, hide:function(duration){this.container.hide(duration);}, fadeIn:function(duration){this.container.fadeIn(duration);}, fadeOut:function(duration){this.container.fadeOut(duration);}, locateAt:function(obj,offsetX,offsetY){ this.container.locate(obj,pj.LEFT_BOTTOM_POSITION,offsetX,offsetY); } }; Calendar.emptySelect=function(target){ if(!target.options)return; while(target.options.length>0)target.remove(0); }; Calendar.fx=function(dig){return dig<10?'0'+dig:dig}; Calendar.isThisMonth=function(date){ return date.getFullYear()==Calendar.prototype.todate.getFullYear()&&date.getMonth()==Calendar.prototype.todate.getMonth(); };
编程素材网打包 在线演示 http://demo.jb51.net/js/Calendar_pj/index.htm[/url] 打包下载 [url=http://www.1sucai.cn/jiaoben/33760.html]http://www.1sucai.cn/jiaoben/33760.html[/url]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部