jQuery的Toggle事件功能已作为1.9版的一部分删除.
我正在使用这个函数:
$('#example').toggle(function() {
do stuff
}, function() {
do stuff
});
Run Code Online (Sandbox Code Playgroud)
现在Toggle Event已经消失了,重现此功能的最佳方法是什么?
mpl*_*jan 12
加载MIGRATE并在那里查看代码
看到我的帖子有关同样的事情
fn.toggle(handler(eventObject),handler(eventObject)...)哪里去了?
我建议他们将其重命名为fn.toggler而不是删除它
这是代码 - 它是一个独立的jQuery插件,可以按原样使用.
jQuery.fn.toggle = function( fn, fn2 ) {
// Don't mess with animation or css toggles
if ( !jQuery.isFunction( fn ) || !jQuery.isFunction( fn2 ) ) {
return oldToggle.apply( this, arguments );
}
// migrateWarn("jQuery.fn.toggle(handler, handler...) is deprecated");
// Save reference to arguments for access in closure
var args = arguments,
guid = fn.guid || jQuery.guid++,
i = 0,
toggler = function( event ) {
// Figure out which function to execute
var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
// Make sure that clicks stop
event.preventDefault();
// and execute the function
return args[ lastToggle ].apply( this, arguments ) || false;
};
// link all the functions, so any of them can unbind this click handler
toggler.guid = guid;
while ( i < args.length ) {
args[ i++ ].guid = guid;
}
return this.click( toggler );
};
Run Code Online (Sandbox Code Playgroud)
更短,未经测试的版本:
(function( $ ){
$.fn.toggler = function( fn, fn2 ) {
var args = arguments,guid = fn.guid || $.guid++,i=0,
toggler = function( event ) {
var lastToggle = ( $._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
$._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
event.preventDefault();
return args[ lastToggle ].apply( this, arguments ) || false;
};
toggler.guid = guid;
while ( i < args.length ) {
args[ i++ ].guid = guid;
}
return this.click( toggler );
};
})( jQuery );
Run Code Online (Sandbox Code Playgroud)
这也很有效.
$.fn.toggleClick = function(){
var functions = arguments ;
return this.click(function(){
var iteration = $(this).data('iteration') || 0;
functions[iteration].apply(this, arguments);
iteration = (iteration + 1) % functions.length ;
$(this).data('iteration', iteration);
});
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16857 次 |
| 最近记录: |