innerHTML/outerHTML; innerText/outerText; textContent
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
outerHTMLproperty was set will still hold a reference to the original element!
- If element has no parent element, set outerHTML will throw DOMException.
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.
- Get: different node types gets different result
- 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
- set
- Browser compatibility!
- get
it = currHeaderChildNodes[i].innerText || currHeaderChildNodes[i].textContent;
- get
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的更多相关文章
- innerHTML,outerHTML,innerText,outerText区别以及insertAdjacentHTML()方法
在需要给文档插入大量的新的HTML标记的情况下,通过多次DOM操作先创建节点再指定它们之间的关系会非常麻烦而且效率不高,相对而言插入标记的方法会更加简单,速度也更快. 插入标记中有这四个属性inner ...
- JS魔法堂:被玩坏的innerHTML、innerText、textContent和value属性
一.前言 由于innerText并非W3C标准属性,因此我们无法在FireFox中使用它(修正:FF45+已经支持innerText属性),一般情况下我们可以使用textContent来代替,但它两者 ...
- [转]被玩坏的innerHTML、innerText、textContent和value属性
一.前言 由于innerText并非W3C标准属性,因此我们无法在FireFox中使用它,一般情况下我们可以使用textContent来代替,但它两者是否就能完全等同呢?在坑爹的表单元素(如input ...
- innerHTML outerHTML innerText
迁移时间--2017年10月31日14:52:59 Author:Marydon UpdateTime--2017年1月15日20:33:03innerHTML,outerHTML与innerTe ...
- innerHTML,innerText,textContent
参考理解 https://www.e-learn.cn/content/html/1765240 https://developer.mozilla.org/zh-CN/docs/Web/API/El ...
- innerHTML,innertext ,textcontent,write()
innerhtml属于对象的一个属性,一般用于向已经存在的标签中写入内容,或者读取标签的内容. innertext属于对象的一个属性,一般只能用于写入内容,或者读取内容,不能读取dom 中的标签,且只 ...
- JS中innerHTML/outerHTML和innerText/outerText以及value 的区别与使用
value value:value是表单元素特有的属性,输入输出的是字符串 如下面的例子,获取到的是他们的value值 <input type="text" id=" ...
- textContent、innerHTML、innerText、outerText、outerHTML、nodeValue使用场景和区别
今天要讲的这些属性都可以用来获取某个元素的内容,你可能会觉得不可思议,或是说上一句"丧心病狂"也.但当你看完以下内容后,会发现除outerText无用外,其他的都有各自的使用场景, ...
- vue-learning:12 - 2 - 区分:outerHTML - innerTHML - outerText - innerText - textContent
区分:outerHTML - innerTHML - outerText - innerText - textContent 获取值 <div id="outer"> ...
随机推荐
- 学习嵌入式Linux有没有一个最佳的顺序(持续更新)
作为一个嵌入式Linux的初学者,我知道我可能将长期处于初学者阶段,因为我至今仍然没有能够摸索出一条很好的道路让我由初学者进入到更高级阶段.但是我始终没有放弃,本篇文章就是用来记录我学习嵌入式Linu ...
- Hdu 2475-Box LCT,动态树
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- mvcc摘抄
MVCC浅析原文:---->>>>>> http://blog.csdn.net/chosen0ne/article/details/18093187 在并发读写数 ...
- oracle AWR深入研究分析,如何使用
AWR的前身是statspack,当然现在还在,只不过大家都在使用AWR,因为它方便,简单,直观,形象. AWR是oracle内置工具,安装oracle时已经自动安装完毕,无需额外安装了. SELEC ...
- Linux内核之mmc子系统-sdio
现在的Linux内核中,mmc不仅是一个驱动,而是一个子系统.这里通过分析Linux3.2.0内核,结合TI的arm335x平台及omap_hsmmcd host分析下mmc子系统,重点关注sdio及 ...
- docker 镜像和容器的批量清理
镜像和容器的清理 删除所有运行中的容器 $ docker kill $(docker ps -q) 删除所有停止的容器 $ docker rm $(docker ps -a -q) 删除所有没有tag ...
- Linux入门基础 #5:Linux文件系统挂载管理
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- android 44 SQLiteOpenHelper
java package com.sxt.day06_10; import java.util.ArrayList; import com.sxt.day06_10.entity.StudentBea ...
- 计算机体系结构-CPU
NUMA
- 亲测apache
http://www.cnblogs.com/bluewelkin/p/3805107.html 里面是纠正了原文的一些小错误,即可正常安装 1.su 命令 2.安装apr-1.3.5.tar.gz ...