亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

暴力使用面向對象解決組件不能復用的問題,同時改進了一點樣式,供大家參考

function?Datepicker()?{
????var?monthDate,?$wrapper,?that?=?this;
????this.getMonthDate?=?function?(year,?month)?{
????????var?ret?=?[];
????????//如果沒有傳入年月,則獲取當前年月
????????if(!year?||?!month){
????????????var?today?=?new?Date();
????????????year?=?today.getFullYear();
????????????month?=?today.getMonth()?+?1;
????????}
????????//獲取本月的第一天所在的年份月份日期
????????var?firstDay?=?new?Date(year,?month?-?1,?1);
????????year?=?firstDay.getFullYear();
????????month?=?firstDay.getMonth()?+?1;
????????//判斷當天是周幾
????????var?firstDayWeekDay?=?firstDay.getDay();
????????//修正星期天顯示為0
????????if(firstDayWeekDay?===?0)?firstDayWeekDay?=?7;
????????//獲取上個月的最后一天
????????var?lastDayOfLastMonth?=?new?Date(year,?month?-?1,?0);
????????var?lastDateOfLastMonth?=?lastDayOfLastMonth.getDate();
????????//獲取上個月應該顯示的天數
????????var?preMonthDayCount?=?firstDayWeekDay?-?1;
????????//獲取當月的最后一天
????????var?lastDay?=?new?Date(year,?month,?0);
????????var?lastDate?=?lastDay.getDate();
????????//循環獲取當前月的每一天
????????for(var?i=0;?i<42;?i++){
????????????//算出真實日期是多少(只看day?不考慮month)
????????????var?date?=?i?-?preMonthDayCount?+1;
????????????var?showDate?=?date;
????????????//指定當前月
????????????var?thisMonth?=?month;
????????????//處理上一月與下一月
????????????if(date?<=?0){
????????????????//上一月
????????????????thisMonth?=?month?-?1;
????????????????showDate?=?lastDateOfLastMonth?+?date;
????????????}else?if(date?>?lastDate){
????????????????//下一月
????????????????thisMonth?=?month?+?1;
????????????????showDate?=?showDate?-?lastDate;
????????????}
????????????//修正月顯示
????????????if(thisMonth?===?0)?thisMonth?=12;
????????????if(thisMonth?===?13)?thisMonth?=?1;
????????????//返回相關數據
????????????ret.push({
????????????????month:thisMonth,
????????????????date:date,
????????????????showDate:showDate
????????????});
????????}
????????return?{
????????????year:year,
????????????month:month,
????????????days:ret
????????};
????};
????this.buildUi?=?function?(year,?month)?{
????????monthDate?=?that.getMonthDate(year,?month);
????????var?html?=?'<div?class="ui-datepicker-header">'?+
????????????'<a?href="#"?class="ui-datepicker-btn?ui-datepicker-prev-btn">&lt;</a>'?+
????????????'<a?href="#"?class="ui-datepicker-btn?ui-datepicker-next-btn">&gt;</a>'?+
????????????'<span?class="ui-datepicker-curr-month">'+?monthDate.year?+?'-'?+?monthDate.month?+?'</span>'?+
????????????'</div>'?+
????????????'<div?class="ui-datepicker-body">'?+
????????????'<table>'?+
????????????'<thead>'?+
????????????'<tr>'?+
????????????'<th>一</th>'?+
????????????'<th>二</th>'?+
????????????'<th>三</th>'?+
????????????'<th>四</th>'?+
????????????'<th>五</th>'?+
????????????'<th>六</th>'?+
????????????'<th>日</th>'?+
????????????'</tr>'?+
????????????'</thead>'?+
????????????'<tbody>';
????????for(var?i=0;?i<42;?i++){
????????????var?date?=?monthDate.days[i];
????????????if(i%7?===?0){
????????????????html?+=?'<tr>';
????????????}
????????????var?nowMonth,?className;
????????????if(Number(date.showDate)?>?Number(date.date)){
????????????????nowMonth?=?monthDate.month?-?1;
????????????????className?=?"ui-datepicker-bggary";
????????????}else?if(date.showDate?===?date.date){
????????????????nowMonth?=?monthDate.month;
????????????????if((new?Date()).getDate()?===?date.date?&&?(new?Date()).getMonth()?+?1?===?monthDate.month?&&?(new?Date()).getFullYear()?===?monthDate.year){
????????????????????className?=?"ui-datepicker-bgnow";
????????????????}else?{
????????????????????className?=?"ui-datepicker-bgdefault";
????????????????}
????????????}else?{
????????????????nowMonth?=?monthDate.month?+?1;
????????????????className?=?"ui-datepicker-bggary";
????????????}
????????????html?+=?'<td?class="'+?className?+'"?data-date="'?+?monthDate.year?+?'-'?+?nowMonth?+?'-'?+?date.showDate?+?'">'?+?date.showDate?+?'</td>';
????????????if(i%7?===?6){
????????????????html?+=?'</tr>';
????????????}
????????}
????????html?+=?'</tbody></table></div>';
????????return?html;
????};
????this.render?=?function?(year,?month)?{
????????var?html?=?that.buildUi(year,?month);
????????if(!$wrapper){
????????????$wrapper?=?document.createElement("div");
????????????$wrapper.className?=?"ui-datepicker-wrapper";
????????????document.body.appendChild($wrapper);
????????}
????????$wrapper.innerHTML?=?html;
????};
????this.init?=?function?(input,?year,?month)?{
????????that.render(year,?month);
????????var?$input?=?document.querySelector(input);
????????$input.addEventListener("click",function?()?{
????????????if($wrapper.classList.contains("ui-datepicker-wrapper-show")){
????????????????$wrapper.classList.remove("ui-datepicker-wrapper-show");
????????????}else?{
????????????????$wrapper.classList.add("ui-datepicker-wrapper-show");
????????????????var?left?=?$input.offsetLeft,
????????????????????top?=?$input.offsetTop,
????????????????????height?=?$input.offsetHeight;
????????????????$wrapper.style.top?=?top?+height?+?2?+?"px";
????????????????$wrapper.style.left?=?left?+?"px";
????????????}
????????},false);
????????$wrapper.addEventListener("click",function?(e)?{
????????????var?$target?=?e.target;
????????????if(!$target.classList.contains("ui-datepicker-btn")){return?false}
????????????if($target.classList.contains("ui-datepicker-prev-btn")){
????????????????//上一月
????????????????if(monthDate.month?===?1){
????????????????????year?=?monthDate.year?-?1;
????????????????????month?=?12;
????????????????}else?{
????????????????????year?=?monthDate.year;
????????????????????month?=?monthDate.month?-?1;
????????????????}
????????????????that.render(year,?month);
????????????}else?{
????????????????//下一月
????????????????if(monthDate.month?===?12){
????????????????????year?=?monthDate.year?+?1;
????????????????????month?=?1;
????????????????}else?{
????????????????????year?=?monthDate.year;
????????????????????month?=?monthDate.month?+?1;
????????????????}
????????????????that.render(year,?month);
????????????}
????????});
????????$wrapper.addEventListener("click",function?(e)?{
????????????var?$target?=?e.target;
????????????if($target.tagName.toLowerCase()?!==?"td"){return?false}
????????????if($target.classList.contains("ui-datepicker-bggary")){return?false}
????????????$input.value?=?$target.getAttribute("data-date");
????????????$wrapper.classList.remove("ui-datepicker-wrapper-show");
????????})
????}
}
var?date1?=?new?Datepicker();
var?date2?=?new?Datepicker();
var?date3?=?new?Datepicker();
date1.init(".m1",?2018,?3);
date2.init(".m2",?2018,?4);
date3.init(".m3",?2018,?5);


正在回答

2 回答

<!--這里調用三個日歷-->
<input?class="m1"?type="text">
<input?class="m2"?type="text">
<input?class="m3"?type="text">


0 回復 有任何疑惑可以回復我~

.ui-datepicker-wrapper{
? ?width: 240px;
? ?font-size: 16px;
? ?color: #666;
? ?box-shadow: 2px 2px 8px 2px rgba(128, 128, 128, 0.3);
? ?position: absolute;
? ?display: none;
? ?background-color: white;
}
.ui-datepicker-wrapper-show{
? ?display: block;
}
.ui-datepicker-wrapper .ui-datepicker-header{
? ?padding: 0 20px;
? ?height: 50px;
? ?line-height:50px;
? ?text-align: center;
? ?background-color: #F0F0F0;
? ?border-bottom:1px solid #CCCCCC;
? ?font-weight: bold;
}
.ui-datepicker-wrapper .ui-datepicker-btn{
? ?font-family: serif;
? ?font-size: 20px;
? ?width: 20px;
? ?height: 50px;
? ?line-height: 50px;
? ?color: #1abc9c;
? ?text-align: center;
? ?text-decoration: none;
}
.ui-datepicker-wrapper .ui-datepicker-prev-btn{
? ?float: left;
}
.ui-datepicker-wrapper .ui-datepicker-next-btn{
? ?float: right;
}
.ui-datepicker-wrapper .ui-datepicker-body table{
? ?width: 100%;
? ?border-collapse: collapse;
}
.ui-datepicker-wrapper .ui-datepicker-body th{
? ?height: 40px;
? ?line-height: 40px;
? ?text-align: center;
? ?font-size: 14px;
}
.ui-datepicker-wrapper .ui-datepicker-body td{
? ?height: 30px;
? ?text-align: center;
? ?font-size: 12px;
? ?font-weight: bold;
? ?border: 1px solid #a0a0a0;
}
/*這里是對不同日期給與不同背景色*/
.ui-datepicker-bgdefault{
? ?color: #1e1e1e;
? ?cursor: pointer;
}
.ui-datepicker-bggary{
? ?background-color: #f0f0f0;
? ?color: #a0a0a0;
}
.ui-datepicker-bgnow{
? ?background-color: #d2d2d2;
? ?color: #000000;
? ?cursor: pointer;
}
.ui-datepicker-bgred{
? ?background-color: #ff0000;
? ?color:white;
}
.ui-datepicker-bghit{
? ?background-color: #00ff00;
}

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

暴力使用面向對象解決組件不能復用的問題,同時改進了一點樣式,供大家參考

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號