¿Cómo detener el burbujeo de eventos en jQuery? [duplicar]


Esta pregunta ya tiene una respuesta aquí:

¿Cómo puedo detener el burbujeo de eventos personalizados en jQuery?

Por ejemplo tengo este código:

$('.myclass').bind('amodaldestroy', function(){
    ....does something.....
})

¿Cómo permitir que esto se active solo una vez en el primer elemento que encuentra cuando ¿burbujeando? ¿Puedo añadir return false?

$('.myclass').bind('amodaldestroy', function(){
     ....does something.....
    return false;
})
Author: informatik01, 2010-12-23

5 answers

De acuerdo con la documentación de jQuery :

$('myclass').bind('amodaldestroy'), function(event) {
    ....does something....
    event.stopPropagation();
});
 43
Author: Daniel T.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-23 20:41:20

Uso event.stopPropagation();

$('.myclass').bind('amodaldestroy', function(e){
    e.stopPropagation();
});

También puedes usar return false pero hay una sutil diferencia entre los dos en que devolver false también llama event.preventDefault();

 13
Author: Russ Cam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-23 20:35:37

Para soporte en IE

$(".element").click(function(e)
{
    var event = e || window.event;
    event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
});
 7
Author: David Sherret,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-02 13:09:13

Esto podría no ser tan grande, pero

return false;

return false hará ambos stopPropagation y event.preventDefault...

Si solo desea uno, puede elegir según su elección

Por favor , lea esto, esto es de SO only

return false es efectivamente tanto preventDefault como stopPropogation.

preventDefault evitará que se produzca el evento predeterminado, stopPropogatio n evitará que el evento burbujee y return false hará ambas cosas.

 3
Author: kobe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-23 20:52:36
 2
Author: Codler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-23 20:32:39