gpt4 book ai didi

toggle - 在 jQuery > 1.8 中使用什么来代替 `toggle(...)`?

转载 作者:行者123 更新时间:2023-12-03 22:17:35 27 4
gpt4 key购买 nike

现在 toggle(...)deprecated in jQuery 1.8然后removed in jQuery 1.9

常规中可以使用什么(除了使用 jQuery 迁移脚本)来代替具有相同类型功能的 toggle(fn, fn2);

相关问题(针对具体案例询问):What to use instead toggle?

<小时/>

知道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, ... );

关于toggle - 在 jQuery > 1.8 中使用什么来代替 `toggle(...)`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com