This question already has answers here:
What's the difference between event.stopPropagation and event.preventDefault?

(8个答案)


4年前关闭。




试图捕获“ bootstrap 选项卡”面板菜单上的按键,但忽略了放置在选项卡的按键按下处理程序上的preventDefault(),它只会冒出气泡。
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();

});

例子:
http://www.bootply.com/xUlN0dLRaV

我在这里想念什么?

最佳答案

试试e.stopPropagation()
e.stopPropagation()防止事件使DOM树冒泡,从而防止任何父处理程序收到该事件的通知。

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

有何不同?

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

关于javascript - keydown上的event.preventDefault()无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42696059/

10-12 02:41