ทั้งสอง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