问题描述
在 shadow DOM 中的元素内使用访问键不会在 Firefox 中触发。
我在 MacOS 上测试了 Chrome 和 Safari,它按预期工作。通过 keydown
或 keyup
事件侦听器手动映射键似乎很复杂,因为键映射因浏览器和操作系统而异。
我创建了简单的小提琴:https://jsfiddle.net/jk3mrx98/
class CustomElement extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
const customTextarea = document.createElement('textarea');
customTextarea.accessKey = 'F';
customTextarea.innerText = 'Accesskey F'
shadowRoot.appendChild(customTextarea);
}
}
window.customElements.define('custom-element',CustomElement);
<textarea accesskey="G">Accesskey G</textarea>
<custom-element></custom-element>
解决方法
很可能是 Firefox 88 中的一个错误,该错误已在即将发布的 89 中修复。
似乎 shadow root 中的 accesskey 从未在当前的 Firefox 稳定版本 (88) 中正确注册,并且 Alt+Shift+F 会在那里触发“文件”菜单。
但它似乎在即将发布的版本中得到修复:在 Windows 上的 Firefox Developer 89.b5(Aurora 发布频道)中尝试了你的小提琴,两个访问键都按预期工作:Alt+Shift+G 聚焦“原生”文本区域,Alt+ Shift+F 聚焦自定义元素中的 textarea。
,没有回答。
只是展示了一种编写构造函数的不同方式:
- 您可以在
---------------------------------------- System information ---------------------------------------- Platform: Windows-10-10.0.16299-SP0 Python: 3.9.1 PyCharm: 2021.1.1 Matplotlib: 3.4.1 NumPy: 1.20.2 OpenCV: 4.5.1
调用之前编写 JS 代码,但还没有“这个” - 你可以链接 super()
super()