jQuery 多事件处理程序 - 如何取消?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/652495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:31:33  来源:igfitidea点击:

jQuery Multiple Event Handlers - How to Cancel?

jqueryjavascript-eventsevent-handling

提问by Josh Stodola

I have two functions bound to a click event at two different times (using jQuery). The order in which they are fired is significant. They are firing in the correct order. The problem is, when the first function returns false, the second function is still firing!

我有两个函数在两个不同的时间绑定到一个点击事件(使用 jQuery)。它们被发射的顺序很重要。他们按正确的顺序开火。问题是,当第一个函数返回 false 时,第二个函数仍在触发!

How can I properly cancel the event?

如何正确取消活动?

Example code:

示例代码:

$(document).click(function() { 
  alert('a');
  return false;
});

$(document).click(function() {
  alert('b');
});

You will still see the "b" alert message when clicking the page. This is unacceptable!

单击页面时,您仍会看到“b”警报消息。这是无法接受的!

回答by Ken Browning

Use the stopImmediatePropagationfunction of the jQuery event object.

使用stopImmediatePropagationjQuery 事件对象的功能。

Keeps the rest of the handlers from being executed. This method also stops the bubbling by calling event.stopPropagation().

阻止其余的处理程序被执行。此方法还通过调用 event.stopPropagation() 来停止冒泡。

$(document).click(function(event) { 
  alert('a');
  event.stopImmediatePropagation();
  return false;
});

$(document).click(function() {
  alert('b');
});

回答by Rodrigo Jara

Thanks, unbind works for redefining functions.

谢谢, unbind 适用于重新定义函数。

  $(document).ready(function(){
        $("#buscar_mercaderia").click(function(){ alert('Seleccione Tipo de mercaderia'); 
   });
$(".tipo").live('click',function(){
            if($(this).attr('checked')!=''){
                if($(this).val()=='libro'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){  window.open('buscar_libro.php','Buscar Libros', 'width=800,height=500'); });
                }else if($(this).val()=='otra'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){ window.open('buscar_mercaderia.php','Buscar Mercaderias', 'width=800,height=500');  });
                }
            }
        })

回答by Seb

The first thing I'm asking is: why do you have twofunctions bound to the same click event? Having access to the code, why don't you just make that one single call?

我要问的第一件事是:为什么将两个函数绑定到同一个点击事件?可以访问代码,为什么不直接打一个电话呢?

$(function (){
    var callbackOne = function(e){
        alert("I'm the first callback... Warning, I might return false!");
        return false;
    };

    var callbackTwo = function(e){
        alert("I'm the second callback");
    };

    $(document).click(function (e){
        if(callbackOne(e)){
            callbackTwo(e);
        }
    });
});

回答by nickf

Does using unbind help?

使用 unbind 有帮助吗?

$(document).click(function() { 
  alert('a');
  $(this).unbind('click');
  return false;
});

回答by fmsf

If what nickf said doesn't work, you could use a small state machine:

如果 nickf 所说的不起作用,您可以使用一个小型状态机:

var trigger = 0;

$(document).click(function() { 

  alert('a');
  if(you_want_to_return_false) {
    trigger = 1;
    return false;
  }
});

$(document).click(function() {
  if(trigger !== 0) {
  alert('b');
  } 
  trigger = 0;
});

Not the prettiest solution, but it will work.

不是最漂亮的解决方案,但它会起作用。