简体   繁体   English

删除特定对象上的 jQuery 委托事件处理程序

[英]Remove jQuery delegated event handler on specific object

I've attached delegated event handlers to a number of elements on the page using a single selector.我使用单个选择器将委托的事件处理程序附加到页面上的许多元素。 As the events are triggered for individual elements, I'd like to turn off only that element's event handler based on some conditional logic .由于事件是针对单个元素触发的,因此我只想根据某些条件逻辑关闭该元素的事件处理程序。 That means I won't necessarily want to disable the event on the very first click .这意味着我不一定要在第一次单击时禁用该事件 But I can't figure out how to do it without turning off all of them.但是我不知道如何在不关闭所有这些的情况下做到这一点。

HTML: HTML:

<button>One</button>
<button>Two</button>
<button>Three</button>

JS: JS:

$(document).on('click', 'button', function(ev) {
    // doesn't work because argument needs to be a string
    $(document).off('click', $(ev.target));

    // doesn't do what I want b/c turns off events on all buttons, not just this one
    $(document).off('click', 'button');

    // doesn't work because event is on document, not button
    $(ev.target).off('click');
});

jQuery's off documentation says I need to provide a string as the second argument, not a jQuery object ( $(ev.target) ), but if I provide a string, there's no value that refers only to the item clicked. jQuery 的关闭文档说我需要提供一个字符串作为第二个参数,而不是一个 jQuery 对象( $(ev.target) ),但是如果我提供一个字符串,则没有仅指单击的项目的值。

From jQuery's off documentation:来自 jQuery 的关闭文档:

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.选择器字符串必须与附加事件处理程序时传递给 .on() 的字符串完全匹配。 To remove all delegated events from an element without removing non-delegated events, use the special value "**".要从元素中删除所有委托事件而不删除非委托事件,请使用特殊值“**”。

So how do I turn off a delegated event handler for a specific element?那么如何关闭特定元素的委托事件处理程序呢?

Here's a JSFiddle of the code above这是上面代码的 JSFiddle

UPDATE : Added a few examples of options that don't work , based on initial answers provided.更新:根据提供的初始答案,添加了一些不起作用的选项示例。

After having read thru on the web, the answer is you can't!在网上通读之后,答案是你不能! You can either remove all or none.您可以删除全部或不删除。 A workaround could be something like the following.解决方法可能类似于以下内容。

$(document).on('click', '.btn', function (ev) {
    alert('pressed');
    $(this).removeClass("btn");
});

Demo@ Fiddle演示@小提琴

Sample HTML:示例 HTML:

<button class="btn">One</button>
<button class="btn">Two</button>
<button class="btn">Three</button>

In addition to what lshettyl said (the current top post) - an additional work around is to bind a new event listener directly to the element that you're trying to remove the listener and call stopPropagation() therein.除了 lshettyl 所说的(当前的顶级帖子)之外 - 一个额外的解决方法是将一个新的事件侦听器直接绑定到您尝试删除侦听器并在其中调用 stopPropagation() 的元素。

What this will do is prevent the event from traveling up the DOM and reaching the event handler that is initially bound to the document.这将阻止事件沿 DOM 向上传播并到达最初绑定到文档的事件处理程序。 Also this will allow you to keep some functionality on the button click, such as an alert to the user that this button has already been clicked - or something to that effect.此外,这将允许您在按钮点击时保留一些功能,例如提醒用户该按钮已被点击 - 或类似的东西。

$(document).on('click', 'button', function(ev) {
    // Your logic to occur on button click

    // Prevent further click on just this button
    $(this).click(function(event) {
        event.stopPropagation();
    }):
});

Question: Do you have to use the delegated events?问题:你必须使用委托事件吗? LIke LShetty said, it is not possible to remove a delegated event for a single element.就像 LShetty 所说的那样,不可能删除单个元素的委托事件。 You either remove the entire event delegation, or leave it.您要么删除整个事件委托,要么保留它。 You could try using a different selector instead like in this example您可以尝试使用不同的选择器,而不是像本例中那样

 $('button').on('click', function(ev) { $('#output').append('Clicked! '); $(this).off('click'); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <button>One</button> <button>Two</button> <button>Three</button> <div id="output"></div>

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

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