简体   繁体   English

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

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

I have section element, which is contenteditable=true, and it contains another elements like images and paragraphs created by user. 我有section元素,它是contenteditable = true,它包含另一个元素,例如用户创建的图像和段落。

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

If I attach jQuery on('keypress') event to the section, or to p element, it doesn't work. 如果我将jQuery on('keypress')事件附加到该部分或p元素,则它将不起作用。 'Click' events work just fine. “点击”事件运行良好。

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

Is it so that in order to get keypress events, the element must be contenteditable enabled? 为了获得按键事件,该元素必须启用contenteditable吗?

You need to attach your event to the section element like so: 您需要将事件附加到section元素,如下所示:

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

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

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

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