¿Cómo detener el burbujeo de eventos en jQuery? [duplicar]
Esta pregunta ya tiene una respuesta aquí:
- ¿Cómo puedo evitar que el evento onclick de un padre se active cuando se hace clic en un ancla hijo? 15 respuestas
¿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;
})
5 answers
De acuerdo con la documentación de jQuery :
$('myclass').bind('amodaldestroy'), function(event) {
....does something....
event.stopPropagation();
});
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
$('.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();
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);
});
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.
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
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