event.preventDefault() on keydown isn't working
本问题已经有最佳答案,请猛点这里访问。
试图捕获引导选项卡面板菜单上的按键,但它会冒泡,忽略放置在选项卡的keydown处理程序上的preventDefault()。
1 2 3 4 5 6 7 8 9 10 | 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
我这里缺什么?
试试
1 2 3 4 5 | $('body > div > ul > li > a').on("keydown",function (e) { console.log("handled by the child - stop bubbling please"); e.preventDefault(); e.stopPropagation(); }); |
差异?
event.stopperopagation和event.preventdefault有什么区别?
除
1 2 3 4 | $('body > div > ul > li > a').on("keydown",function (e) { console.log("handled by the child - stop bubbling please"); return false; }); |