How to add onkeydown to body?

后端 未结 3 1355
离开以前
离开以前 2021-01-18 07:08

In my index.html I have this code. I would need to add onkeydown event to that body in Main.onLoad().

Any idea how to do it?



        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-18 07:22

    Usually onkeydown can be attached to the elements which can receive focus. In some browsers you can gain focus to the document.body, but you can't rely on that. window is one of those elements which can gain the focus.

    You can try to put the code below to the head section of your HTML code, after TVA is defined.

    window.onkeydown = TVA.keyDown;
    

    Or when using addEventListener():

    window.addEventListener('keydown', TVA.keyDown, false);
    

提交回复
热议问题