How to bind keyboard events to div elements?

前端 未结 4 1440
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 04:55

Is there a way to listen to keyboard events on a DIV element?

My code:

相关标签:
4条回答
  • 2020-12-05 05:25

    Why not place the input inside the div and then simply find $('#input').closest('div') ?

    Otherwise, if your example is a pattern of how your inputs relate to your divs then simply $('#input').prev() ?

    0 讨论(0)
  • 2020-12-05 05:28

    Add a tabindex and it should work

    <div id="div" style="height:50px;" class="ui-widget-content" tabindex="1"></div>
    

    DEMO

    0 讨论(0)
  • 2020-12-05 05:29

    you need to add a tabindex to the div.

    see this fiddle:

    http://jsfiddle.net/w2Y4d/1/

    0 讨论(0)
  • 2020-12-05 05:46

    You can add a tabindex in that div to catch keyboard events like this

    <div id="div" style="height:50px" class="ui-widget-content" tabindex="0"></div>
    

    Like answered here.

    Working Fiddle

    Reference

    0 讨论(0)
提交回复
热议问题