IE innerHTML error

后端 未结 3 885
孤街浪徒
孤街浪徒 2020-12-20 02:59

This is a little different than the questions that have already been asked on this topic. I used that advice to turn a function like this:

function foo() {

         


        
3条回答
  •  天命终不由人
    2020-12-20 03:00

    You can create your own shim/polyfill of .innerHTML. I have just made some code to fix another problematic aspect of .innerHTML in IE: the clearing of child .innerHTMLs:

    You can modify the behavior. Here is some code that prevents garbage collection of otherwise-referenced elements in IE:

    if (/(msie|trident)/i.test(navigator.userAgent)) {
     var innerhtml_get = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerHTML").get
     var innerhtml_set = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerHTML").set
     Object.defineProperty(HTMLElement.prototype, "innerHTML", {
      get: function () {return innerhtml_get.call (this)},
      set: function(new_html) {
       var childNodes = this.childNodes
       for (var curlen = childNodes.length, i = curlen; i > 0; i--) {
        this.removeChild (childNodes[0])
       }
       innerhtml_set.call (this, new_html)
      }
     })
    }
    
    var mydiv = document.createElement ('div')
    mydiv.innerHTML = "test"
    document.body.appendChild (mydiv)
    
    document.body.innerHTML = ""
    console.log (mydiv.innerHTML)
    

    http://jsfiddle.net/DLLbc/9/

提交回复
热议问题