Why does IE give unexpected errors when setting innerHTML

后端 未结 10 2740
面向向阳花
面向向阳花 2020-12-10 17:20

I tried to set innerHTML on an element in firefox and it worked fine, tried it in IE and got unexpected errors with no obvious reason why.

For example if you try and

相关标签:
10条回答
  • 2020-12-10 18:04

    I have been battling with the problem of replacing a list of links in a table with a different list of links. As above, the problem comes with IE and its readonly property of table elements.

    Append for me wasn't an option so I have (finally) worked out this (which works for Ch, FF and IE 8.0 (yet to try others - but I am hopeful)).

    replaceInReadOnly(document.getElementById("links"), "<a href>........etc</a>");
    
    function replaceInReadOnly(element, content){
        var newNode = document.createElement();
        newNode.innerHTML = content;
        var oldNode = element.firstChild;
        var output = element.replaceChild(newNode, oldNode);
    }
    

    Works for me - I hope it works for you

    0 讨论(0)
  • 2020-12-10 18:08

    Don't know why you're being down-modded for the question Stu, as this is something I solved quite recently. The trick is to 'squirt' the HTML into a DOM element that is not currently attached to the document tree. Here's the code snippet that does it:

    // removing the scripts to avoid any 'Permission Denied' errors in IE
    var cleaned = html.replace(/<script(.|\s)*?\/script>/g, "");
    
    // IE is stricter on malformed HTML injecting direct into DOM. By injecting into 
    // an element that's not yet part of DOM it's more lenient and will clean it up.
    if (jQuery.browser.msie)
    {
        var tempElement = document.createElement("DIV");
        tempElement.innerHTML = cleaned;
        cleaned = tempElement.innerHTML;
        tempElement = null;
    }
    // now 'cleaned' is ready to use...
    

    Note we're using only using jQuery in this snippet here to test for whether the browser is IE, there's no hard dependency on jQuery.

    0 讨论(0)
  • 2020-12-10 18:09

    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/

    0 讨论(0)
  • 2020-12-10 18:10

    "Apparently firefox isn't this picky" ==> Apparently FireFox is so buggy, that it doesn't register this obvious violation of basic html-nesting rules ...

    As someone pointed out in another forum, FireFox will accept, that you append an entire html-document as a child of a form-field or even an image ,-(

    0 讨论(0)
提交回复
热议问题