最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

innerHTML/outerHTML;innerText/outerText;textContent-GentleMint

来源:动视网 责编:小采 时间:2020-11-27 16:44:39
文档

innerHTML/outerHTML;innerText/outerText;textContent-GentleMint

innerHTML/outerHTML;innerText/outerText;textContent-GentleMint: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 cont
推荐度:
导读innerHTML/outerHTML;innerText/outerText;textContent-GentleMint: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 cont


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:
    // This is a div.
    
    container = document.getElementById("container");
    d = document.getElementById("d");
    console.log(container.firstChild.nodeName); // logs "DIV"
    
    d.outerHTML = "

    This paragraph replaced the original div.

    "; 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.
  • FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (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!
  • 文档

    innerHTML/outerHTML;innerText/outerText;textContent-GentleMint

    innerHTML/outerHTML;innerText/outerText;textContent-GentleMint: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 cont
    推荐度:
    标签: html text inner
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top