Tanto innerText
y textContent
están estandarizados a partir de 2016. Todos los Node
objetos (incluyendo los nodos de texto puro) tienen textContent
, pero sólo los HTMLElement
objetos tienen innerText
.
Si bien textContent
funciona con la mayoría de los navegadores, no funciona en IE8 o versiones anteriores. Use este polyfill para que funcione solo en IE8. Este polyfill no funcionará con IE7 o versiones anteriores.
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);
}
}
);
})();
}
El Object.defineProperty
método está disponible en IE9 o superior, sin embargo, está disponible en IE8 solo para objetos 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