Differentiate between focus event triggered by keyboard/mouse

后端 未结 4 1454
有刺的猬
有刺的猬 2020-12-03 16:41

I\'m using jquery ui autocomplete and want to decipher between focus events triggered by keyboard interaction and mouse interaction. How would I go about this?



        
4条回答
  •  我在风中等你
    2020-12-03 17:26

    You should actually be able to determine this from the event-Object that is passed into the focus-event. Depending on your code structure this might be different, but there is usually a property called originalEvent in there, which might be nested to some depth. Examine the event-object more closely to determine the correct syntax. Then test on mousenter or keydown via regular expression. Something like this:

    focus: function(event, ui){
      if(/^key/.test(event.originalEvent.originalEvent.type)){
        //code for keydown
      }else{
        //code for mouseenter and any other event
      }
    }
    

提交回复
热议问题