javascript - 如何在模式打开时停止触发 TAB 键事件

标签 javascript html css angular ngx-bootstrap

当模式显示时,如何禁用 TAB 等按键事件?我在页面上的表单中有多个按钮和字段,可以通过 Tab 键选择(聚焦)。我想在模式显示时禁用它。该应用程序使用 Angular,模态框位于子组件中。

<button tabindex="0">Button1</button>
<button tabindex="0">Button2</button>
<form>...</form>

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">...</div>
  </div>
</div>

最佳答案

您可以添加自己的事件监听器:

// Your event listener
function preventTab(e) {
  e = e || window.event;
  if (e.keyCode === 9) { // If tab key is pressed
    e.preventDefault() // Stop event from its action
  }
}

// Call this when modal window opens
document.addEventListener(preventTab);

// Call this when modal window closes/unmounts
document.removeEventListener(preventTab);

关于javascript - 如何在模式打开时停止触发 TAB 键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48382618/

相关文章:

javascript - 这是 JS array.find 的错误使用还是需要忽略的 VSCode 警告?

php - 需要向 html 表添加一个删除按钮,以通过一条消息从数据库中删除记录

html - 设计具有 3D 效果的 table

html - 马赛克风格的图像网格、 float 和边距

css - 背景图片只显示一次

javascript - 无法在 chrome 扩展中使用 jQuery

javascript - 如果按 Interval(Continue btn) 两次或更多次,则 AnglularJS Interval 不会暂停

javascript - 仅在 IE9 标准模式下的 asp.net javascript 内存泄漏

Jquery Accordion 菜单主动打开

javascript获取子div的滚动