替代jQuery的.toggle支持EVENTDATA()方法?

编程入门 行业动态 更新时间:2024-10-27 12:40:46
本文介绍了替代jQuery的.toggle支持EVENTDATA()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为 .toggle()法国 jQuery的文档

时,提供了方便的.toggle()方法。这是比较容易实现通过手相同的行为,并且如果假设内置.toggle()证明限制性这可能是必要的。

The .toggle() method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into .toggle() prove limiting.

内置 .toggle 的假设已经被证明限制了我目前的任务,但文档没有就如何落实相同的行为阐述。我需要EVENTDATA传递给提供给切换()的处理功能,但似乎只有 .bi​​nd()将支持这一点,不是 .toggle()。

The assumptions built into .toggle have proven limiting for my current task, but the documentation doesn't elaborate on how to implement the same behavior. I need to pass eventData to the handler functions provided to toggle(), but it appears that only .bind() will support this, not .toggle().

我的第一个倾向是使用一个标志,是全球单个处理函数存储点击状态。换句话说,而不是

My first inclination is to use a flag that's global to a single handler function to store the click state. In other words, rather than:

$('a').toggle(function() { alert('odd number of clicks'); }, function() { alert('even number of clicks'); });

做到这一点:

var clicks = true; $('a').click(function() { if (clicks) { alert('odd number of clicks'); clicks = false; } else { alert('even number of clicks'); clicks = true; } });

我没有测试后,但我怀疑它会工作。这是做这样的事情最好的办法,还是有我就是缺少一个更好的办法?

I haven't tested the latter, but I suspect it would work. Is this the best way to do something like this, or is there a better way that I'm missing?

谢谢!

推荐答案

似乎是一个合理的方式来做到这一点...我只是建议你使用jQuery的的data存储事业,而不是引入一个额外的变量(这可能会成为一个头疼的,如果你想跟踪一大堆的链接)。因此,根据您的例子:

Seems like a reasonable way to do it... I'd just suggest that you make use of jQuery's data storage utilities rather than introducing an extra variable (which could become a headache if you wanted to keep track of a whole bunch of links). So based of your example:

$('a').click(function() { var clicks = $(this).data('clicks'); if (clicks) { alert('odd number of clicks'); } else { alert('even number of clicks'); } $(this).data("clicks", !clicks); });

更多推荐

替代jQuery的.toggle支持EVENTDATA()方法?

本文发布于:2023-11-07 14:42:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   jQuery   toggle   EVENTDATA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!