on方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| kjQuery.prototype.extend({ on: function (name, callBack) { this.each(function (key, ele) { if (!ele.eventsCache) { ele.eventsCache = {}; } if (!ele.eventsCache[name]) { ele.eventsCache[name] = []; ele.eventsCache[name].push(callBack); kjQuery.addEvent(ele, name, function () { kjQuery.each(ele.eventsCache[name], function (k, method) { method(); }); }); } else { ele.eventsCache[name].push(callBack); } }); }, });
|
将事件名称作为一个对象的键来存储,具体事件放到该键对应的数组中。
off方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| off: function (name, callBack) { if (arguments.length == 0) { this.each(function (key, ele) { ele.eventsCache = {}; }); } else if (arguments == 1) { this.each(function (key, ele) { ele.eventsCache[name] = []; }); } else if (arguments.length == 2) { this.each(function (key, ele) { kjQuery.each(ele.eventsCache[name], function (index, method) { if (method === callBack) { ele.eventsCache[name].splice(index, 1); } }); }); } },
|