xpyusrs
V2EX  ›  Vue.js

请问怎么监听一个 Modal 弹窗的键盘事件

  •  
  •   xpyusrs · Jun 9, 2022 · 2121 views
    This topic created in 1440 days ago, the information mentioned may be changed or developed.

    目前只会用 @keyup 监听输入框的键盘事件

    2 replies    2022-06-09 17:48:36 +08:00
    stillsilly
        1
    stillsilly  
       Jun 9, 2022
    绑在 document 或者 window 上,然后在函数里判断弹窗是否是打开状态。
    或者打开弹窗的时候绑事件,关闭弹窗的时候取消。
    document.onkeydown = function(){console.log('document')}
    window.onkeydown = function(){console.log('window')}
    peterczg
        2
    peterczg  
       Jun 9, 2022
    楼上说的有道理,可以优化一下,尽量不要重写 document.onxxx ,如果不是自己的项目,可能会重写别人的回调。

    建议楼主多看看 MDN ,深入了解一下 Web 原生相关的 JS API 。

    ducment.addEventListener('keypress', function(){})
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5317 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 08:15 · PVG 16:15 · LAX 01:15 · JFK 04:15
    ♥ Do have faith in what you're doing.