Jquery切换两个函数在1.10.1中不起作用
Jquery切换两个函数在jquery 1.10.1中不起作用,但在Jquery 1.8.3上工作
HTML
For example, consider the HTML:
Click here
jQuery的
$('#target').toggle(function() { alert('First handler for .toggle() called.'); }, function() { alert('Second handler for .toggle() called.'); });
和expmple在这里
老兄,它在jQuery 1.10.1中不起作用
还有另一种方法可以做到这一点……
$(function () { function first() { //Code for first time click goes here $(this).one("click", second); } function second() { //Code for second time click goes here $(this).one("click", first); } $("#target").one("click", first); });
jQuery.toggle自1.8版以来已被弃用,稍后会删除。 您可以编写自己的实现:
function handler1() { alert("first handler for .toggle() called."); $(this).one("click", handler2); } function handler2() { alert("second handler for .toggle() called."); $(this).one("click", handler1); } $("#target").one("click", handler1);
演示
DEMO
DEMO jQuery 2.x(边缘)
使用此clickToggle
插件作为toggle
function从版本1.9中删除
(function ($) { $.fn.clickToggle = function (func1, func2) { var funcs = [func1, func2]; this.data('toggleclicked', 0); this.click(function () { var data = $(this).data(); var tc = data.toggleclicked; $.proxy(funcs[tc], this)(); data.toggleclicked = (tc + 1) % 2; }); return this; }; }(jQuery)); $('#target').clickToggle(function () { alert('First handler for .toggle() called.'); }, function () { alert('Second handler for .toggle() called.'); });
在1.9版本中删除了此格式,因此您无法使用它 – 如果您希望手动实现此逻辑,或者您可以使用迁移插件
包含jQuery库添加后包含迁移插件
它在jQuery 1.9中删除了:
这是“单击运行指定函数的元素”.toggle()的签名。 它不应该与.toggle()的“更改元素的可见性”相混淆,而不是已弃用。 前者被删除以减少混淆并提高库中模块化的可能性。 jQuery Migrate插件可用于恢复function。
完整的升级指南: http : //jquery.com/upgrade-guide/1.9/
这是一个通用方法,具有多个要切换的函数:
var toggleFunctions = [ function () { /* do something */}, function () { /* do something else */}, ... function () { /* do some other stuff*/} ]; $("#target").on("click", function(){ // returns first function from array var currentFunction = toggleFunctions.shift(); // executes the function currentFunction(); // pushes current function to the back toggleFunctions [] = currentFunction; });
切换在jquery中对我不起作用所以在stackoverflow的帮助下我的替代方案是