本文介绍了使用什么代替jQuery>中的`toggle(...)`. 1.8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在toggle(...)
在jQuery 1.8中已被不推荐使用,然后在jQuery 1.9中 已删除
Now that toggle(...)
was deprecated in jQuery 1.8 and then removed in jQuery 1.9
通用(除了使用jQuery迁移脚本之外)可以使用什么代替toggle(fn, fn2);
那具有相同类型的功能?
What could be used in general (aside from using the jQuery migrate script) instead of toggle(fn, fn2);
thats has the same type of functionality?
相关问题(询问特定案例):使用什么代替切换?
Related question (asked about a specific case): What to use instead toggle?
我知道 toggle()
功能并未被删除,只是添加了自定义切换功能(除了显示/隐藏默认功能).
I know that toggle()
functionality was not removed, just the ability to add custom toggle functions (aside from the show/hide default functionality).
推荐答案
这是一个简单的实现:
$.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, ... );
这篇关于使用什么代替jQuery>中的`toggle(...)`. 1.8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!