开发者

preventDefault does not behave equal in different browsers

开发者 https://www.devze.com 2023-03-31 16:21 出处:网络
I use jQuery preventDefault on a keydown event: http://jsbin.com/ixaqok/edit#javascript,html When running the example code in Fire开发者_StackOverflowfox and Opera the keypress event still is fired, b

I use jQuery preventDefault on a keydown event: http://jsbin.com/ixaqok/edit#javascript,html When running the example code in Fire开发者_StackOverflowfox and Opera the keypress event still is fired, but in Chrome, IE8 and Safari it's not.

Why? Is preventDefault not supposed to work the same in all browsers?

Thanks!


The problem is that unlike other browsers, Opera only allows you to suppress the default behaviour of keystrokes in the keypress event, not the keydown event. Therefore the solution is to use the keypress event instead.

For more details, use what I consider the definitive page on the mess that is browser key events: http://unixpapa.com/js/key.html. Suppressing default key behaviour is section 2.3.


Opera has problem with recognizing and resolving keydown (and some other key releated funcitons)

see:
http://www.quirksmode.org/dom/events/keys.html
http://api.jquery.com/event.preventDefault/ (in discussion)

0

精彩评论

暂无评论...
验证码 换一张
取 消