unicorn/prefer-dom-node-text-content 樣式
作用
強制對 DOM 節點使用 .textContent
而非 .innerText
。
為什麼這不好?
使用 .innerText 有一些缺點。
.innerText
效能較差,因為它需要版面配置資訊才能返回結果。.innerText
僅針對 HTMLElement 物件定義,而.textContent
則針對所有 Node 物件定義。.innerText
不是標準的,例如,它不存在於 Firefox 中。
範例
此規則的錯誤程式碼範例
javascript
const text = foo.innerText;
此規則的正確程式碼範例
javascript
const text = foo.textContent;