最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

events - javascript preventdefault kills keypress - Stack Overflow

programmeradmin2浏览0评论

using chrome 15 on osx 10.6.8

When adding event listeners, I've found that putting preventDefault in when a keypress event is used, I get mas issues.

Take a look here

unment the line with event.preventDefault(); on it and you'll see the keypress event stops working. what's the deal?

using chrome 15 on osx 10.6.8

When adding event listeners, I've found that putting preventDefault in when a keypress event is used, I get mas issues.

Take a look here

unment the line with event.preventDefault(); on it and you'll see the keypress event stops working. what's the deal?

Share Improve this question asked Dec 24, 2011 at 0:12 JacksonkrJacksonkr 32.3k42 gold badges189 silver badges289 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

preventDefault() does exactly what it's supposed to. It stops the default action of the key. If you want the default action of the key, then don't call it.

In both Chrome and Firefox, I see repeated keypresses, even when preventDefault() is unmented from your fiddle. Perhaps you were losing focus.

It would help us know what to suggest in it's place if you describe more about what you're trying to acplish.

In your jsFiddle, you are listening to keys at the document level. By the time the keystrokes propogate up to the document, they have already been processed by the individual objects that they are targetted at. If you want to intercept and prevent keys from being processed, you have to process them in event handlers on the objects they originally are targetted at. You can't prevent default on keys from the document level. In this jsFiddle where I intercept the keystrokes on the first object they go to, you can see that they are entirely blocked. You may also want to be aware that cross-browser behavior is not perfect in this regard as there are some browser differences you can see here.

发布评论

评论列表(0)

  1. 暂无评论