JQuery html() vs. innerHTML

后端 未结 7 1929
不知归路
不知归路 2020-11-28 22:08

Can I completely rely upon jQuery\'s html() method behaving identical to innerHTML? Is there any difference between innerHTML and jQue

相关标签:
7条回答
  • 2020-11-28 22:45

    "This method uses the browser's innerHTML property." - jQuery API

    http://api.jquery.com/html/

    0 讨论(0)
  • 2020-11-28 22:46

    If you're wondering about functionality, then jQuery's .html() performs the same intended functionality as .innerHTML, but it also performs checks for cross-browser compatibility.

    For this reason, you can always use jQuery's .html() instead of .innerHTML where possible.

    0 讨论(0)
  • 2020-11-28 22:50

    To answer your question:

    .html() will just call .innerHTML after doing some checks for nodeTypes and stuff. It also uses a try/catch block where it tries to use innerHTML first and if that fails, it'll fallback gracefully to jQuery's .empty() + append()

    0 讨论(0)
  • 2020-11-28 22:50

    Here is some code to get you started. You can modify the behavior of .innerHTML -- you could even create your own complete .innerHTML shim. (P.S.: redefining .innerHTML will also work in Firefox, but not Chrome -- they're working on it.)

    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-11-28 22:51

    innerHTML is not standard and may not work in some browsers. I have used html() in all browsers with no problem.

    0 讨论(0)
  • 2020-11-28 22:57

    Given the general support of .innerHTML these days, the only effective difference now is that .html() will execute code in any <script> tags if there are any in the html you give it. .innerHTML, under HTML5, will not.

    From the jQuery docs:

    By design, any jQuery constructor or method that accepts an HTML string — jQuery(), .append(), .after(), etc. — can potentially execute code. This can occur by injection of script tags or use of HTML attributes that execute code (for example, <img onload="">). Do not use these methods to insert strings obtained from untrusted sources such as URL query parameters, cookies, or form inputs. Doing so can introduce cross-site-scripting (XSS) vulnerabilities. Remove or escape any user input before adding content to the document.

    Note: both .innerHTML and .html() can execute js other ways (e.g the onerror attribute).

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