noNoninteractiveElementInteractions
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/a11y/noNoninteractiveElementInteractions¥Diagnostic Category:
lint/a11y/noNoninteractiveElementInteractions -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "a11y": { "noNoninteractiveElementInteractions": "error" } } }}¥Description
禁止在非交互式元素上使用事件处理程序。
¥Disallow use event handlers on non-interactive elements.
非交互式 HTML 元素表示用户界面中的内容和容器。非交互式元素包括 <main>、<area>、<h1>(<h2> 等)、<img>、<li>、<ul> 和 <ol>。
¥Non-interactive HTML elements indicate content and containers in the user interface.
Non-interactive elements include <main>, <area>, <h1> (,<h2>, etc), <img>, <li>, <ul> and <ol>.
非交互式元素不支持事件处理程序(鼠标和键盘处理程序)。
¥A Non-interactive element does not support event handlers(mouse and key handlers).
¥Examples
¥Invalid
<div onClick={() => {}}>button</div>code-block.jsx:1:1 lint/a11y/noNoninteractiveElementInteractions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Non-interactive element should not have event handler.
> 1 │ <div onClick={() => {}}>button</div>
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Consider replace semantically interactive element like <button/> or <a href/>.
¥Valid
<button onClick={() => { }}>button</button>// Adding a role to element does not add behavior.// If not used semantic HTML elements like `button`, developers need to implement the expected behavior for role(like focusability and key press support)// See https://www.w3.org/WAI/ARIA/apg/<div role="button" onClick={() => { }}>button</div>// The role="presentation" attribute removes the semantic meaning of an element, indicating that it should be ignored by assistive technologies.// Therefore, it's acceptable to add event handlers to elements with role="presentation" for visual effects or other purposes,// but users relying on assistive technologies may not be able to interact with these elements.<div role="presentation" onClick={() => { }}>button</div>// Hidden from screen reader.<div onClick={() => {}} aria-hidden />// Custom component is not checked.<SomeComponent onClick={() => {}}>button</SomeComponent>// Spread attributes is not supported.<div {...{"onClick":() => {}}}>button</div>可访问性指南
Section titled “可访问性指南”¥Accessibility guidelines
¥Resources
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号