javascript - 有没有一种快速的方法可以从类或类型的所有元素中删除选项卡焦点?
问题描述
假设我有 2 个按钮,一个输入和一个文本区域,我知道如何设置tabindex="-1"
以防止分别为每个按钮设置标签。
有没有一种快速的方法可以防止我体内的所有按钮都具有选项卡焦点,或者一个类的所有元素(例如类“foo”的所有元素)?
我也对 js 建议持开放态度。
<html>
<body>
<button>BUTTON 1</button><button class="foo">BUTTON 2</button>
<input class="foo">
<textarea></textarea>
</body>
</html>
解决方案
以编程方式设置怎么样tabindex="-1"
?
var unfocusableElems = document.querySelectorAll('button, input, textarea, .foo');
unfocusableElems.forEach(function (el) { el.setAttribute('tabindex', '-1'); });
<html>
<body>
<button>BUTTON 1</button><button class="foo">BUTTON 2</button>
<input class="foo">
<textarea></textarea>
</body>
</html>
推荐阅读
- lisp - “冒号后的非法终止字符:#\”在portacle中,但代码中没有冒号
- c++ - 如何使用断言对数组进行测试?
- haskell - 如何在 Dhall 中表示“Data.Map Text Text”?
- r - purrr 与 pluck 相对(丢弃元素)+ 在 pluck 中保留列表结构
- javascript - 如何用玩笑测试更新组件状态的函数
- python-3.x - Pandas read_csv 方法在使用 ISO 8859-15 编码时无法正确获取“-”字符
- containers - 服务网格与 IBM IIB 或 Oracle ESB 等 2010 年 ESB 解决方案有何不同
- google-chrome - 无法在 chrome 扩展中加载 tensorflowJS 模型
- python - Miniconda 更新到新的 bash 会话
- c# - 如何在 C# 中以异步方式等待多个 WaitHandle 数组并支持取消?