Intercepting all key presses with jQuery before they are processed

前端 未结 2 1697
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 14:01

I have a web page and I need to intercept all keypresses before they get dispatched to whatever element in the dom has the focus. When the user types something like ~

2条回答
  •  再見小時候
    2021-01-19 14:42

    read this about event bubbling. http://www.quirksmode.org/js/events_order.html

    this works for firefox:

     document.addEventListener('keypress',function(e){alert('body1'); e.cancelBubble  = true;},true)
    

    I'm not sure what will work for i.e. as the document i linked to says in i.e. the event bubbles from inwards upwards. you may have to try a different way round this. e.g. you could add an event handler to all the element that you do not want them handling the event and then prevent them handling the event in that event handler.

提交回复
热议问题