V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xpyusrs
V2EX  ›  Vue.js

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

  •  
  •   xpyusrs · 2022-06-09 15:12:00 +08:00 · 1246 次点击
    这是一个创建于 659 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    ducment.addEventListener('keypress', function(){})
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5396 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:35 · PVG 15:35 · LAX 00:35 · JFK 03:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.