jQuery有关事件绑定的函数太丰富了,今天由于某需求,需要一种每次点击触发不同的回调函数的效果,因此瞄一瞄jQuery的API,发现toggle正是我要的,于是也给我框架添一个。但jQuery的实现太复杂了,闭包套嵌过多,我换另一种思路轻松实现了它。
下面是jQuery的实现:
proxy: function ( fn, proxy, thisObject ) { |
if ( arguments.length === 2 ) { |
if ( typeof proxy === "string" ) { |
fn = thisObject[ proxy ]; |
} else if ( proxy && !jQuery.isFunction( proxy ) ) { |
return fn.apply( thisObject || this , arguments ); |
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; |
var args = arguments, i = 1; |
while ( i < args.length ) { |
jQuery.proxy( fn, args[ i++ ] ); |
return this .click( jQuery.proxy( fn, function ( event ) { |
var lastToggle = ( jQuery.data( this , "lastToggle" + fn.guid ) || 0 ) % i; |
jQuery.data( this , "lastToggle" + fn.guid, lastToggle + 1 ); |
return args[ lastToggle ].apply( this , arguments ) || false ; |
这是我的实现:
var callback = function (event){ |
var self = arguments.callee; |
self[ "fn" +self._toggle].call( this ,event) |
self[ "fn" +self._toggle].call( this ,event) |
for ( var i=0;i<arguments.length;i++){ |
callback[ "fn" +i] = arguments[i]; |
return this .click(callback) |
由于我不可能把我的事件模块放出来,它与jQuery的事件模块一样精致与复杂,换言之,一样易碎,强行分割就动弹不了。不过结合我早期放出的addEvent函数,还是能做出来的,虽然事件队列与事件对象还没有现整化。
var addEvent = function (el, type, fn) { |
if (el.addEventListener) { |
el.addEventListener(type, fn, false ); |
} else if (el.attachEvent) { |
el.attachEvent( 'on' + type, function (){ |
var toggle = function (el ){ |
var callback = function (event){ |
var self = arguments.callee; |
self[ "fn" +self._toggle].call( this ,event) |
self[ "fn" +self._toggle].call( this ,event) |
var fns = [].slice.call(arguments,1); |
for ( var i=0;i<fns.length;i++){ |
callback[ "fn" +i] = fns[i]; |
addEvent(el, "click" ,callback); |
var toggle2 = function (el) { |
var fns = [].slice.call(arguments, 1), |
addEvent(el, 'click' , function (e){ |
if (!fns.length) { fns = backup.concat()} |
用法与例子:
(责任编辑:admin) |