简体   繁体   中英

How to capture key events on a simple div?

If we want to capture a key event on a textbox or a contenteditable div we can just use the "addEventListener" method with "keypress", "keyup" events.. but..

If i want to capture the "keyup" event on a simple div element without the "contenteditable" attribute? how can i do this?

You can use the tabindex attribute, so the div it's focusable. Here's a fiddle:

https://jsfiddle.net/87p6r7w6/1/

The user should focus the div first, or you can do it using .focus()

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