detect key press on modal dialog not working

前端 未结 3 1251
离开以前
离开以前 2020-12-20 04:34

I would like to detect whether the user has pressed any key when a modal is shown. I tried the following code but the events are not fired.

Code snippet:

         


        
3条回答
  •  醉酒成梦
    2020-12-20 04:43

    I find it a better solution to give focus to the modal.

    In order to do that, you have to add a tabindex parameter to your modal container, and then set its focus using JavaScript. Once that's done it can receive keyboard events: https://stackoverflow.com/a/6633271/2873507

提交回复
热议问题