Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
event.preventDefault() on keydown isn't working

Trying to capture the key presses on a Bootstrap Tab Panel menus, but it just bubbles up ignoring the preventDefault() placed on the tabs's keydown handler.

document.onkeydown = function(e) {
  console.log("document catched the keydown event");

};

$('body > div > ul > li > a').on("keydown",function (e) {
  console.log("handled by the child - stop bubbling please");
  e.preventDefault();

});

Example: http://www.bootply.com/xUlN0dLRaV

what am i missing here ?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try e.stopPropagation()

e.stopPropagation() prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

$('body > div > ul > li > a').on("keydown",function (e) {
  console.log("handled by the child - stop bubbling please");
  e.preventDefault();
  e.stopPropagation();
});

Difference?

What's the difference between event.stopPropagation and event.preventDefault?

about 4 years ago · Santiago Trujillo Relatório

0

Besides e.preventDefault you have to also use e.stopPropagation() to prevent event bubling up. In your case you can also return false from event handler which do both:

$('body > div > ul > li > a').on("keydown",function (e) {
  console.log("handled by the child - stop bubbling please");
  return false;
});
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda