首页 新闻 搜索 专区 学院

jquery 插件扩展书写问题

0
悬赏园豆:40 [已解决问题] 解决于 2016-12-22 11:41
  1 (function ($) {
  2     var curdate = new Date(),
  3         curyear = curdate.getFullYear(),
  4         curmonth = curdate.getMonth() + 1;
  5     var Methods = {
  6         init: function (settings) {
  7             var self = this;
  8             $(self).empty();
  9             self.opt = $.extend(true, {}, $.fn.dajauCalendar.defaults, settings);
 10             var html = Methods.ghtml(self.opt, curyear, curmonth);
 11             var calendar = $(html);
 12 
 13             $(calendar).find("#pmonth").bind("click", function () {
 14                 curmonth = curmonth == 1 ? 12 : curmonth;
 15                 curyear = curmonth == 1 ? curyear - 1 : curyear;
 16                 Methods.init.apply(self, settings);
 17             });
 18             $(calendar).find("#nmonth").bind("click", function () {
 19                 curmonth = curmonth == 12 ? 1 : curmonth;
 20                 curyear = curmonth == 12 ? curyear + 1 : curyear;
 21                 Methods.init.apply(self, settings);
 22             });
 23 
 24             var data = self.opt.data || {};
 25             calendar = Methods.setdata(calendar, data);
 26             $(self).html(calendar);
 27         },
 28         ghtml: function (settings, y, m) {
 29             curyear = y || curyear;
 30             curmonth = m || curmonth;
 31             var week = ["日", "一", "二", "三", "四", "五", "六"];
 32             //计算每月多少天
 33             function getDays(y, m) {
 34                 y = y || curyear;
 35                 m = m || curmonth;
 36                 if (m == 2) {
 37                     return y % 4 == 0 ? 29 : 28;
 38                 } else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
 39                     return 31;
 40                 } else {
 41                     return 30;
 42                 }
 43             }
 44             //计算某天是星期几
 45             function getWeeks(d) {
 46                 d = d || curdate;
 47                 return week[d.getDay()];
 48             }
 49             var _date1 = new Date(curyear, curmonth, 1); //当月第一天 兼容IE var date= new Date(yyyy,MM,dd)
 50             var idx1 = _date1.getDay();
 51             var _date2 = new Date(curyear, curmonth, getDays()); //当月最后一天
 52             var idx2 = _date2.getDay();
 53             var pm = curmonth == 1 ? 12 : curmonth - 1;
 54             var py = curmonth == 1 ? curyear - 1 : curyear;
 55             var nm = curmonth == 12 ? 1 : curmonth + 1;
 56             var ny = curmonth == 12 ? curyear + 1 : curyear;
 57             var html = "<div class='f_data'>";
 58             html += "<table class='cfrq_a cfrq1' cellpadding='0' cellspacing='0' style='width:" + settings.width + "px;height:" + settings.height + "px'>";
 59             html += "<tbody>";
 60             html += "<tr class='up_bj'>";
 61             html += "<td colspan='7'><span class='left_rq'><a id='pmonth' href='javascript:void()'><img src='" + settings.path + settings.pimg + "' /></a></span>";
 62             html += "<span class='midder_rq'><strong>" + curmonth + "月" + curyear + "</strong></span>";
 63             html += "<span class='right_rq'><a id='nmonth' href='javascript:void()'><img src='" + settings.path + settings.nimg + "' /></a></span></td>";
 64             html += "</tr>";
 65             html += "<tr class='xiqi'>";
 66             for (var i = 0; i < week.length; i++) {
 67                 html += "<td width='100'>" + week[i] + "</td>";
 68             };
 69             html += "</tr>";
 70             for (var i = 1; i <= getDays(); i++) {
 71                 var id = "";
 72                 var _date = new Date(curyear, curmonth, i);
 73                 var idx = _date.getDay();
 74                 if (i === 1) {
 75                     html += "<tr flag='week'>";
 76                     for (var m = 0; m < idx1; m++) {
 77                         if (pm.toString().length === 1)
 78                             id = py + "0" + pm + (getDays(py, pm) - m);
 79                         else
 80                             id = py + "" + pm + (getDays(py, pm) - m);
 81                         html += "<td flag='day' id='" + id + "'><div class='weizhi'><span flag='dayTxt'></span></div></td>";
 82                     };
 83                 }
 84                 var _i = i.toString().length === 1 ? "0" + i : i + "";
 85                 if (curmonth.toString().length === 1)
 86                     id = curyear + "0" + curmonth + _i;
 87                 else
 88                     id = curyear + "" + curmonth + _i;
 89                 if (idx === 6) {
 90                     html += "<td flag='day' id='" + id + "'><div class='weizhi'><span flag='dayTxt'>" + i + "</span></div></td>";
 91                     html += "</tr>"; //结束行
 92                     if (i != getDays())
 93                         html += "<tr flag='week'>"; //新行
 94                 }
 95                 else {
 96                     html += "<td flag='day' id='" + id + "'><div class='weizhi'><span flag='dayTxt'>" + i + "</span></div></td>";
 97                 }
 98                 if (i === getDays() && idx != 6) {
 99                     for (var n = idx2; n < 6; n++) {
100                         if (nm.toString().length === 1)
101                             id = ny + "0" + nm + "0" + n;
102                         else
103                             id = ny + "" + nm + "0" + n;
104                         html += "<td flag='day' id='" + id + "'><div class='weizhi'><span flag='dayTxt'></span></div></td>";
105                     };
106                     html += "</tr>"
107                 }
108             };
109             html += "</tbody>";
110             html += "</table>";
111             html += "</div>";
112             return html;
113         },
114         setdata: function (obj, data) {
115             if (data) {
116                 var d = data.datas;
117                 if (d && d.length > 0) {
118                     for (var m = 0; m < d.length; m++) {
119                         var id = d[m].id;
120                         var value = d[m].value;
121                         obj.find("#" + id).html(value);
122                     }
123                 }
124                 var e = data.events;
125                 if (e && e.length > 0) {
126                     for (var n = 0; n < e.length; n++) {
127                         var id = e[n].id;
128                         var value = e[n].value;
129                         if (typeof value === "function")
130                             obj.find("#" + id).bind("click", value);
131                     };
132                 }
133             }
134             return obj;
135         }
136     }
137     $.fn.dajauCalendar = function (method) {
138         if (Methods[method]) {
139             return Methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
140         } else if (typeof method === "object" || !method)
141             return Methods.init.apply(this, arguments);
142     }
143     $.fn.dajauCalendar.defaults = {
144         width: 450,
145         height: 375,
146         data: {},
147         click: false,
148         path: "/JS/Calandar/Images/",
149         pimg: "ico9.jpg",
150         nimg: "ico10.jpg"
151     }
152 })(jQuery)
日历插件js

上边是我写的日历插件的js代码。在页面上引用js后直接$("#xxx").dajauCalendar()。是可以用了。但是在点击上月和下月的时候就会有错误。我知道错误的地方,不知道怎么去修改这块的js。请会的人能够看下我的代码给我指出一点意见,帮我解惑一下。甚是感谢。。

是下边这块代码的问题。但是我不知道怎么修改。求给提示。。

王元勋的主页 王元勋 | 菜鸟二级 | 园豆:413
提问于:2014-08-28 11:11
< >
分享
最佳答案
0

错误是啥?

收获园豆:40
幻天芒 | 高人七级 |园豆:36790 | 2014-08-28 11:26

王元勋 | 园豆:413 (菜鸟二级) | 2014-08-28 11:43

@RunningMan1229: 这是语法错误呀~~~代码可能不在你这段代码中哦~

幻天芒 | 园豆:36790 (高人七级) | 2014-08-28 11:59

@幻天芒: 错误应该就是我截图那段的错误,应该是传值的错误。但是我不知道怎么去修改。能麻烦你花点时间帮忙看下吗?谢谢。

王元勋 | 园豆:413 (菜鸟二级) | 2014-08-28 14:22

@RunningMan1229: 在事件注册函数中,增加return false,防止事件冒泡。你代码的逻辑问题,你就自己解决吧。

幻天芒 | 园豆:36790 (高人七级) | 2014-08-28 17:24

@幻天芒: 

$(calendar).find("#pmonth").bind("click", function () {
curmonth = curmonth == 1 ? 12 : curmonth;
curyear = curmonth == 1 ? curyear - 1 : curyear;
Methods.init.apply(self, settings);
return false;
});

幻天芒 | 园豆:36790 (高人七级) | 2014-08-28 17:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册