Cả hai innerText& textContentđược chuẩn hóa kể từ năm 2016. Tất cả Nodecác đối tượng (bao gồm các nút văn bản thuần túy) có textContent, nhưng chỉ HTMLElementcác đối tượng có innerText.
Mặc dù textContenthoạt động với hầu hết các trình duyệt, nó không hoạt động trên IE8 hoặc sớm hơn. Sử dụng polyfill này để nó chỉ hoạt động trên IE8. Polyfill này sẽ không hoạt động với IE7 hoặc sớm hơn.
if (Object.defineProperty
&& Object.getOwnPropertyDescriptor
&& Object.getOwnPropertyDescriptor(Element.prototype, "textContent")
&& !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get) {
(function() {
var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
Object.defineProperty(Element.prototype, "textContent",
{
get: function() {
return innerText.get.call(this);
},
set: function(s) {
return innerText.set.call(this, s);
}
}
);
})();
}
Các Object.definePropertyphương pháp là availabe trong IE9 hoặc lên, tuy nhiên nó có sẵn trong IE8 cho đối tượng DOM chỉ.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent