项目里用的easyui,用weekpicker的话需要与easyui代码有冲突会导致页面报错,所以直接改造一下easyui的datebox实现这一功能
1 <input type="text" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser,onSelect:onSelect" id="S_Week">
1 function myformatter(date){ 2 var y = date.getFullYear(); 3 var m = date.getMonth()+1; 4 var d = date.getDate(); 5 return y+'第'+moment(date).week()+'周_'+y+'/'+m+'/'+d; 6 } 7 function myparser(s){ 8 if (!s) return new Date(); 9 var date = s.split('_')[1] 10 var ss = (date.split('/')); 11 var y = parseInt(ss[0],10); 12 var m = parseInt(ss[1],10); 13 var d = parseInt(ss[2],10); 14 if (!isNaN(y) && !isNaN(m) && !isNaN(d)){ 15 return new Date(y,m-1,d); 16 } else { 17 return new Date(); 18 } 19 } 20 function onSelect(date){ 21 var week = moment(date).week() 22 const specifiedWeek = moment().isoWeek(week) 23 const w_StartDate = specifiedWeek.startOf('week').format('YYYY-MM-DD'); 24 const w_EndDate = specifiedWeek.endOf('week').format('YYYY-MM-DD'); 25 $('#S_StartDate').datebox('setValue', w_StartDate);//开始时间input框 26 $('#S_EndDate').datebox('setValue', w_EndDate);//结束时间input框 27 var parentElement = $('.calendar-selected').parent(); 28 var childElements = parentElement.children(); 29 // 遍历子节点并输出 30 childElements.each(function() { 31 $(this).addClass('calendar-selected') 32 }); 33 } 34 function getDayDiv(t){ 35 var day=$(t).closest(".calendar-day"); 36 if(day.length){ 37 return day; 38 }else{ 39 return $(t); 40 } 41 }; 42 $(function () { 43 var calendarDiv = $('#S_Week').datebox().datebox('calendar') 44 calendarDiv.on("mouseover",function(e){ 45 var t=getDayDiv(e.target) 46 if(t.hasClass("calendar-nav")||t.hasClass("calendar-text")||(t.hasClass("calendar-day")&&!t.hasClass("calendar-disabled"))){ 47 t.addClass("calendar-nav-hover") 48 var parentElement = t.parent(); 49 var childElements = parentElement.children(); 50 childElements.each(function() { 51 $(this).addClass('calendar-nav-hover') 52 }); 53 } 54 }).on("mouseout",function(e){ 55 var t=getDayDiv(e.target); 56 if(t.hasClass("calendar-nav")||t.hasClass("calendar-text")||(t.hasClass("calendar-day")&&!t.hasClass("calendar-disabled"))){ 57 t.removeClass("calendar-nav-hover"); 58 var parentElement = t.parent(); 59 var childElements = parentElement.children(); 60 childElements.each(function() { 61 $(this).removeClass('calendar-nav-hover') 62 }); 63 } 64 }) 65 66 $('#S_Week').datebox({ 67 onShowPanel: function() { 68 // 在打开面板前进行一些预处理操作 69 var parentElement = $('.calendar-selected').parent(); 70 var childElements = parentElement.children(); 71 72 childElements.each(function() { 73 $(this).addClass('calendar-selected') 74 }); 75 76 } 77 }); 78 })
标签:function,parentElement,easyui,js,date,var,calendar,选择器,datebox From: https://www.cnblogs.com/jiajiaxu/p/17967068