keydown上的event.preventDefault()不起作用
问题描述:
试图捕获Bootstrap Tab Panel菜单上的按键,但它只是冒起来忽略放在标签的keydown处理程序上的preventDefault()。keydown上的event.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.preventDefault
你还必须使用e.stopPropagation()
来防止事件冒泡。你的情况,你也可以从事件处理程序返回false
这一举两得:
$('body > div > ul > li > a').on("keydown",function (e) {
console.log("handled by the child - stop bubbling please");
return false;
});
答
尝试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?
+0
当你确定一个问题是重复的,而不是回答时,发表评论说它是重复的,以便人们可以正确地关闭它。 (当然,在约1400点的代表中,你可以直接投下相近的选票。) –
e.stopPropagation()? –
你需要使用'stopPrepagation'作为回答,这显然不是重复的。我们的伴侣问为什么他使用的功能不起作用,而不是与其他功能有什么不同。 –