yearTag等变量不要依赖onScrollEnd中的取值,改成在sureBtn.onclick中实时获取
try { indexY = Math.ceil((yearScroll.y || 0) / 40 * -1 + 1); yearTag = yearUl.getElementsByTagName("li")[indexY].getAttribute("data-year"); } catch (err) { yearTag = that.beginYear } try { indexM = Math.ceil((monthScroll.y || 0) / 40 * -1 + 1); monthTag = monthUl.getElementsByTagName("li")[indexM].getAttribute("data-month") } catch (err) { monthTag = 1 } try { indexD = Math.ceil((dayScroll.y || 0) / 40 * -1 + 1); dayTag = dayUl.getElementsByTagName("li")[indexD].getAttribute("data-day") } catch (err) { dayTag = 1 }
原因产生:插件内部初始化的参数默认传入当前日期作为结束值endXXX,我们自定义时同样把当前日期作为开始值beginXXX,导致插件createDateYMD函数在type == "month"时判断出现了开始值大于结束值,但是渲染dom时按照for循环的i++模式取不到值。
if (type == "month") { unitName = "月"; dataStyle = "data-month"; beginNum = that.beginMonth; endNum = 12; if (yearTag != that.beginYear) { beginNum = 1 } if (yearTag == dateopts.endYear) { endNum = that.endMonth } } for (var i = beginNum; i <= endNum; i++) { str += "<li " + dataStyle + "=" + i + ">" + that.dateForTen(i) + unitName + "</li>" }
格式化源码并查看,如下:
includeCss("Mdate/needcss/Mdate.css"); var dateopts = { beginYear: 2000, beginMonth: 1, beginDay: 1, endYear: new Date().getFullYear(), endMonth: new Date().getMonth() + 1, endDay: new Date().getDate(), format: "YMD" }
解决办法一:
去除插件初始化endXXX的动态日期为固定日期
var dateopts = { beginYear: 2000, beginMonth: 1, beginDay: 1, endYear: 2099, endMonth: 1, endDay: 1, format: "YMD" };
解决办法二:
渲染dom结构的for循环先判断下初始月份和结束月份值的大小,按照i++从小到大的顺序写