Оба innerText& textContentстандартизированы по состоянию на 2016 год. Все Nodeобъекты (включая узлы чистого текста) имеют textContent, но только HTMLElementобъекты имеют innerText.
Хотя textContentработает с большинством браузеров, он не работает на IE8 или более ранних версиях. Используйте этот polyfill, чтобы он работал только в IE8. Этот polyfill не будет работать с IE7 или ранее.
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);
}
}
);
})();
}
Этот Object.definePropertyметод доступен в IE9 или более поздней версии, однако он доступен в IE8 только для объектов DOM.
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