【发布时间】:2022-01-23 02:44:43
【问题描述】:
我试图让一个 div 及其所有后代无法聚焦。我在根 div 上使用了 tabIndex -1。但这使焦点转移到了它的第一个可聚焦的孩子身上。 (默认行为)是否有解决此问题的方法?
示例代码
const SomeFunctionComponent = () => {
// ... some logic ...
const someTrustedHtml = `<p>Hello <a href="stackoverflow.com">World!</a></p>`;
const content = React.createElement("div", {
className: "some class name",
dangerouslySetInnerHTML: {
__html: someTrustedHtml;
},
tabIndex: -1, /* "tabindex": -1 does not work */
// None of these options helped either
// "data-is-focusable": false,
// "aria-hidden": true,
// disabled: true,
// role: "presentation"
});
// ... more logic ...
return (
<div>
{content}
<button onClick={() => console.log("hello world")}></button>
{/* some tab focusable elements */}
</div>
);
}
预期行为:
当SomeFunctionComponent 获得焦点时,焦点会移动到按钮上
实际行为: 焦点转到危险的SetInnerHtml中的链接
【问题讨论】:
标签: javascript html reactjs focus accessibility