现在,切换(…)是
deprecated in jQuery 1.8,然后是
removed in jQuery 1.9
一般可以使用(除了使用jQuery迁移脚本),而不是toggle(fn,fn2);那些具有相同的功能类型?
相关问题(有关具体情况):What to use instead toggle?
解决方法
这是一个简单的实现:
$.fn.toggleClick = function() { var methods = arguments; // Store the passed arguments for future reference var count = methods.length; // Cache the number of methods // Use return this to maintain jQuery chainability // For each element you bind to return this.each(function(i,item){ // Create a local counter for that element var index = 0; // Bind a click handler to that element $(item).on('click',function() { // That when called will apply the 'index'th method to that element // the index % count means that we constrain our iterator between 0 // and (count-1) return methods[index++ % count].apply(this,arguments); }); }); };
并将其用作
$('selector').toggleClick( function1,function2,... );