繁体   English   中英

jQuery keypress事件不适用于contenteditable部分中的元素

[英]jQuery keypress event doesn't work on elements inside contenteditable section

我有section元素,它是contenteditable = true,它包含另一个元素,例如用户创建的图像和段落。

<section contenteditable=true>
<p>Hello there</p>
</section>

如果我将jQuery on('keypress')事件附加到该部分或p元素,则它将不起作用。 “点击”事件运行良好。

这是jsFiddle: http : //jsfiddle.net/j9mHF/

为了获得按键事件,该元素必须启用contenteditable吗?

您需要将事件附加到section元素,如下所示:

$('section').on('keypress', function(e) {
   alert('typed'); 
});

http://jsfiddle.net/Dieterg/j9mHF/1/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM