gpt4 book ai didi

jquery - 关闭选择中特定项目的事件

转载 作者:行者123 更新时间:2023-12-01 03:17:30 25 4
gpt4 key购买 nike

假设我有这样的东西:

$('#mydiv').on('click', 'button', doSomething);

显然我希望所有按钮在点击时都doSomething()。现在假设我希望其中一个按钮不执行此操作,而是执行其他操作。

$('#mydiv').off('click', '.specialclass button');

这不起作用,但我需要它。有办法实现这一点吗?我非常希望不必单独连接每个按钮。

这部分.off() documentation 是什么让我丧命:

To remove specific delegated event handlers, provide a selector argument. The selector string must exactly match the one passed to .on() when the event handler was attached.

Here's a fiddle演示我正在尝试做的事情。

最佳答案

您可以使用 jQuery 伪选择器 :not() 来排除搜索结果,它是文档 here .

$('#mydiv').on('click', 'button:not(.specialclass)', doSomething);

那么你可以单独指定具体情况:

$('.specialclass').on('click', function() { console.log('sup'; });
<小时/>

您还可以使用 unbind 来关闭 jQuery 中的事件。

$('div').unbind('click');​

虽然没有弃用,但根据文档:

As of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements

关于jquery - 关闭选择中特定项目的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13963983/

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