Skip to content
← Back to rules

unicorn/prefer-dom-node-text-content 风格

🛠️ An auto-fix is available for this rule for some violations.

它做了什么

强制对 DOM 节点使用 .textContent 代替 .innerText

为什么这是不好的?

使用 .innerText 存在一些缺点。

  • .innerText 的性能开销更大,因为它需要布局信息才能返回结果。
  • .innerText 仅定义于 HTMLElement 对象上,而 .textContent 则适用于所有 Node 对象。
  • .innerText 并非标准特性,例如在 Firefox 中并不存在。

示例

此规则的错误代码示例:

javascript
const text = foo.innerText;

此规则的正确代码示例:

javascript
const text = foo.textContent;

如何使用

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

json
{
  "rules": {
    "unicorn/prefer-dom-node-text-content": "error"
  }
}
bash
oxlint --deny unicorn/prefer-dom-node-text-content

参考资料