Skip to content
← Back to rules

jsx-a11y/iframe-has-title 正确性

它的作用

强制要求 iframe 元素必须包含 title 属性。

为什么这是个问题?

屏幕阅读器用户依赖 iframetitle 属性来了解其内容。如果标记中缺少 title 属性,用户在导航 iframe 及其内部元素时会变得困难且困惑。

示例

以下为 不正确 的代码示例:

jsx
<iframe />
<iframe {...props} />
<iframe title="" />
<iframe title={''} />
<iframe title={``} />
<iframe title={undefined} />
<iframe title={false} />
<iframe title={true} />
<iframe title={42} />

以下为 正确 的代码示例:

jsx
<iframe title="这是一个唯一标题" />
<iframe title={uniqueTitle} />

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["jsx-a11y"],
  "rules": {
    "jsx-a11y/iframe-has-title": "error"
  }
}
bash
oxlint --deny jsx-a11y/iframe-has-title --jsx-a11y-plugin

参考资料