需要绕过jquery的toggle功能
我需要能够绕过切换的第一个功能,我想知道 jquery 切换是如何工作的?
据我所知,如果我点击某个东西,就会执行第一个功能,然后当我再次点击它时,就会执行第二个功能。
我需要做的是绕过第一个函数并直接进入第二个函数。
我这样做的原因是按钮的状态已被发生的另一个事件更改。现在,当我第一次单击该按钮时,它假定这是第一次单击该按钮,并且当我需要它执行第二个功能时,它将执行第一个功能。
有办法解决这个问题吗?如果 jQuery 切换函数等于 true 或 false,那么我肯定应该能够检查它并调用正确的脚本?
非常感谢!
I need to be able to bypass the first function of a toggle and I was wondering how the jquery toggle works?
I understand that if I click on something, the first function is carried out and then when I click on it again, the second function is carried out.
What I need to be able to do is to bypass the first function and go directly to the second one.
Reason I am doing this is that the state of a button has been changed by another event that's happened. When I now click on that button for the first time, it assumes that it's the first time it's been clicked and will carry out the first function when I need it to carry out the second function.
Is there a way around this? If the jQuery toggle function equates to a true or false, then surely I should be able to check this and call on the correct script?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设另一个事件并不总是在第一次切换之前触发,否则您可以反转这些功能。
您可以更改其他事件来触发切换吗?如果其他事件正在执行相同的任务,那么这将是要走的路。
否则,您可以使用
.data()
在元素上放置一个标志来检查toggle()
是否已触发。如果没有,则进行第一个功能测试以查看该元素是否已被其他事件修改。I assume that the other event doesn't always fire before the first toggle, otherwise you could just reverse the functions.
Could you change your other event to fire the toggle? If the other event is performing the same task, then this would be the way to go.
Otherwise, you could use
.data()
to place a flag on the element to check if thetoggle()
has fired yet. If it hasn't, then have the first function test to see if the element has been modified by the other event.您可以在切换函数之外创建一个变量:
var isFirstToggle = true;
。然后在您的切换回调中,如果等于 true,则返回;
让我知道您的想法以及这是否合适。可能有更干净的方法来处理它,但这应该可以快速完成。
You could create a variable outside of the toggle function:
var isFirstToggle = true;
.Then in your toggle callback just return if it is equal to true;
Let me know your thoughts and if this seems appropriate. There is probably cleaner ways to handle it, but this should work quick-n-dirty.