innerHTML/outerHTML; innerText/outerText; textContent,outerhtmloutertext

來源:互聯網
上載者:User

innerHTML/outerHTML; innerText/outerText; textContent,outerhtmloutertext
innerHTML v.s. outerHTML

  • Element.innerHTML
    •   Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
    •   Functionality
      •   Get serialized HTML code describing its descendants.
      •   Set : Remove all the children, parse the content string and assign the resulting nodes as the children of the element. 
  • Element.outerHTML
    •   Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML
    •   Functionality
      •   Get serialized HTML fragment describling the element and its descendants.
      •   Set : Replace the element with the nodes generated by parsing the content string with parent of the element as the context node for the fragment parsing algorithm.
    •   NOTE
      •   If element has no parent element, set outerHTML will throw DOMException.
        •   e.g. [Chrome Dev Console]  document.documentElement.outerHTML='a';   Uncaught DOMException: Failed to set the 'outerHTML' property on 'Element': This element's parent is of type '#document', which is not an element node.
      •   Considering below code.
        // HTML:// <div id="container"><div id="d">This is a div.</div></div>container = document.getElementById("container");d = document.getElementById("d");console.log(container.firstChild.nodeName); // logs "DIV"d.outerHTML = "<p>This paragraph replaced the original div.</p>";console.log(container.firstChild.nodeName); // logs "P"// The #d div is no longer part of the document tree,// the new paragraph replaced it.

        While the element will be replaced in the document, the variable whose outerHTML property was set will still hold a reference to the original element!

innerText and outerText
  • Node.innerText
    •   Non-standard: DO NOT use it on production site.
  • HTMLElement.outerText
    •   Non-standard: DO NOT use it on production site.
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 45 (45) 6 9.6 (probably earlier) 3
 textContent v.s innerText
  • Node.textContent
    • Get: different node types gets different result
      •   null: document, notation (use document.documentElement.textContent instead).
      •   text inside the node: CDATA, comment, text node, processing instruction. (nodeValue)
      •   concatenation of children nodes (excluding comment, processing instruction nodes) text: other types node
    • Set: Remove node children and replace it with a text node.
  • Difference from innerText
    •   many... : refer to MDN.
  • Why we still need innerText sometime?
    •   Browser compatibility!
      •   IE has better support for innerText than for textContent. Only IE9+ supports textContent, but IE6+ supports innerText.
    •   Common usage:
      •   set
        t[t.innerText ? 'innerText' : 'textContent'] = v.n

         

 

      •   get
        it = currHeaderChildNodes[i].innerText || currHeaderChildNodes[i].textContent;

         

 

textContent v.s. innerHTML
  • It's recommand to use textContent!
    • innerHTML parse text as HTML (except "script" element) -> poor performance!
    • innerHTML has security problem!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.