'innerText' works in IE, but not in Firefox

前端 未结 15 1409
执念已碎
执念已碎 2020-11-21 05:01

I have some JavaScript code that works in IE containing the following:

myElement.innerText = \"foo\";

However, it seems that the \'innerTex

15条回答
  •  迷失自我
    2020-11-21 05:52

    It's also possible to emulate innerText behavior in other browsers:

     if (((typeof window.HTMLElement) !== "undefined") && ((typeof HTMLElement.prototype.__defineGetter__) !== "undefined")) {
         HTMLElement.prototype.__defineGetter__("innerText", function () {
             if (this.textContent) {
                 return this.textContent;
             } else {
                 var r = this.ownerDocument.createRange();
                 r.selectNodeContents(this);
                 return r.toString();
             }
         });
         HTMLElement.prototype.__defineSetter__("innerText", function (str) {
             if (this.textContent) {
                 this.textContent = str;
             } else {
                 this.innerHTML = str.replace(/&/g, '&').replace(/>/g, '>').replace(/\n");
             }
         });
     }
    

提交回复
热议问题