innerHTML/outerHTML; innerText/outerText; textContent - GentleMint

php中文网
发布: 2016-05-20 13:46:50
原创
1862人浏览过

innerHTML v.s. outerHTML

  • Element.innerHTML
  • 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.
        <span style="color: #008000;">//</span><span style="color: #008000;"> HTML:</span><span style="color: #008000;">
        //</span><span style="color: #008000;"> <div id="container"><div id="d">This is a div.</div></div></span>
        <span style="color: #000000;">
        container </span>= document.getElementById("container"<span style="color: #000000;">);
        d </span>= document.getElementById("d"<span style="color: #000000;">);
        console.log(container.firstChild.nodeName); </span><span style="color: #008000;">//</span><span style="color: #008000;"> logs "DIV"</span>
        <span style="color: #000000;">
        d.outerHTML </span>= "<p>this paragraph replaced the original div.</p>"<span style="color: #000000;">;
        console.log(container.firstChild.nodeName); </span><span style="color: #008000;">//</span><span style="color: #008000;"> logs "P"</span>
        
        <span style="color: #008000;">//</span><span style="color: #008000;"> The #d div is no longer part of the document tree,</span><span style="color: #008000;">
        //</span><span style="color: #008000;"> the new paragraph replaced it.</span>
        登录后复制

        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

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:

 

立即学习前端免费学习笔记(深入)”;

 

立即学习前端免费学习笔记(深入)”;

textContent v.s. innerHTML

  • It's recommand to use textContent!
    • innerHTML parse text as HTML (except "script" element) -> poor performance!
    • innerHTML has security problem!
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号