KeyboardEvent 的 stopPropagation()
问题
如何成功防止 KeyboardEvent 冒泡,而不立即在事件处理程序中执行 return false
?
注意:该事件的类型为 KeyboardEvent
并且我没有使用 jQuery,因此 e.stopPropagation()
不是一个选项(没有当我 console.log
时,在 KeyboardEvent
对象上使用这种方法)。
更多信息
事件使用本机方法(addEventListener()
或 attachEvent()
,具体取决于浏览器)绑定,并且默认值被取消使用此函数:
this.cancelHandler = function(event) {
/**
* Cross browser event cancellation (e.preventDefault() is not available without jQuery
*/
var e = event || window.event;
// All good browsers…
if (e.preventDefault) {e.preventDefault();}
// …and IE
if (e.returnValue) {e.returnValue = false;}
return false;
};
防止触发默认值,但它不会停止这些 KeyboardEvent
的传播。
我正在寻找至少与 IE7+ 和现代浏览器兼容的东西。
Question
How can I successfully prevent a KeyboardEvent from bubbling, without doing a return false
immediately in the event handler?
Note: the event is of type KeyboardEvent
and I'm not using jQuery, so e.stopPropagation()
is not an option (there's not such method on the KeyboardEvent
object when I console.log
it).
More info
The events are bound using native methods (addEventListener()
or attachEvent()
, depending on the browser), and the defaults are canceled using this function:
this.cancelHandler = function(event) {
/**
* Cross browser event cancellation (e.preventDefault() is not available without jQuery
*/
var e = event || window.event;
// All good browsers…
if (e.preventDefault) {e.preventDefault();}
// …and IE
if (e.returnValue) {e.returnValue = false;}
return false;
};
That prevents the defaults from firing, but it doesn't stop propagation for those KeyboardEvent
s.
I'm looking for something compatible with at least IE7+ and modern browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这段代码
stopPropagation 不是 jQuery,而是标准函数(当然不是标准函数)
Use this piece of code
stopPropagation is NOT jQuery, but standard function ( certainly NOT ie standard )