简体   繁体   中英

jQuery keydown event event data

I am in the process of upgrading our application's jQuery version from 1.4.2. I have a chunk of JS, which I don't see why it would work but in fact works in 1.4.2 and not in 1.4.3+

$(document).bind('keydown', 'f3',
           function (event) {
               alert("f3");
               //Do something
});

in jQuery 1.4.2 this WORKS and triggers the event handler only for F3. When I upgrade to 1.4.3+ the event handler is triggered for any keydown (which I think makes sense).

Does the keydown event know to use the event data and check if the key was pressed?

Can anyone help me clarify if it does or does not, and if not why would this code be working in jQuery 1.4.2? I checked the release notes and the only thing that changed is added method signatures for the bind and keydown events.

Yes. The event object has all information regarding event. But you need to check for F3 manually using properties such as keyCode and which and such properties.

For example the keyCode for F3 is 114. So you would check it like this:

if(e.keyCode === 114){
  //F3 is pressed
}

It might be working for you still because now, the second(optional) argument is eventData to which you're passing 'f3'

Yes you can check for the key property and you should be doing it as follows:

$(document).keydown(function( event ) {
  if ( event.which == 114 ) { // 114 is the identifier for F3
    //Do some stuff
    event.preventDefault();
  }
});

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