Skip to content
← Back to rules

unicorn/prefer-dom-node-text-content Style

🛠️ 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;

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-dom-node-text-content": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-dom-node-text-content": "error",
  },
});
bash
oxlint --deny unicorn/prefer-dom-node-text-content

版本

此规则在 v0.0.21 中新增。

参考资料