简体   繁体   English

jQuery Toggle —一种功能一键单击然后另一种

[英]jQuery Toggle — One Function One Click Then Another

Here's what I would like to do in pseudocode form. 这是我想以伪代码形式进行的操作。

Bind Two Functions to Click Event

On 1st/Odd Clicks Do FunctionA()

On 2nd/Even Clicks Do FunctionB()

The .toggle() function seems to not be designed for this. .toggle()函数似乎并非为此设计的。

I'm sure this exists in jQuery but my googling hasn't resulted in anything. 我确定这存在于jQuery中,但是我的谷歌搜索没有任何结果。 :) :)

I don't see how this isn't exactly what toggle is designed for... 我不明白这与toggle功能设计的目的不完全相同 ...

$('#someEl').toggle(FunctionA, FunctionB);

You may be confused by the other toggle function that exists in jQuery, which won't be called if you pass functions as the arguments. 您可能会对jQuery中存在的另一个 toggle功能感到困惑,如果您将功能作为参数传递,则不会被调用。

var i=1;
$(".a").click (function () {if (i%2!=0) FunctionA();i++;});
$(".a").click (function () {if (i%2==0) FunctionB();i++;});

You just need to ask Google the right question ;) 您只需要问Google正确的问题即可;)

$(selector).toggle(function(),function(),function(),...)
  • function() Required. function()必需。 Specifies a function to run every EVEN time the element is clicked 指定一个函数,该函数每单击一次元素就会运行一次
  • function() Required. function()必需。 Specifies a function to run every ODD time the element is clicked 指定一个函数,该函数在单击元素时每隔奇数次运行一次
  • function(),... Optional. function(),...可选。 Specifies additional functions to toggle between 指定在以下功能之间切换的其他功能

http://www.w3schools.com/jquery/eff_toggle.asp http://www.w3schools.com/jquery/eff_toggle.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM