简体   繁体   中英

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>
<p>Hello there</p>
</section>

If I attach jQuery on('keypress') event to the section, or to p element, it doesn't work. 'Click' events work just fine.

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

Is it so that in order to get keypress events, the element must be contenteditable enabled?

You need to attach your event to the section element like so:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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