document对象中有innerhtml、innertext这两个属性,都是获取document对象文本内容,但使用起来还是有区别的;
1) innerhtml设置或获取标签所包含的html+文本信息(从标签起始位置到终止位置全部内容,包括html标签,但不包括自身)
2) outerhtml设置或获取标签自身及其所包含的html+文本信息(包括自身)
3) innertext设置或获取标签所包含的文本信息(从标签起始位置到终止位置的内容,去除html标签,但不包括自身)
4) outertext设置或获取标签自身及其所包含的文本信息(包括自身)
innertext和outertext在获取的时候是相同效果,但在设置时,innertext仅设置标签所包含的文本,而outertext设置包含包括标签自身在内的文本。
示例代码:
示例1:
通过ie浏览器打开,弹出内容为"hello world"和"hello world"
通过firefox浏览器打开,弹出内容为"hello world"和"undefined"
通过chrome浏览器打开,弹出的内容为"hello world"和"hello world"
alert(content.outerhtml)则弹出:"<p id="p1">hello world</p>"
示例2
通过ie浏览器打开,弹出内容为"<p id="p1">hello world</p>"和"hello world"
通过firefox浏览器打开,弹出内容为"<p id="p1">hello world</p>"和"undefined"
通过chrome浏览器打开,弹出的内容为"<p id="p1">hello world</p>"和"hello world"
alert(content.outerhtml)则弹出:"<div id="d1"><p id="p1">hello world</p></div>"
综上:innerhtml所有浏览器都支持,innertext是ie浏览器支持的,firefox浏览器不支持。
不同之处:
1) innerhtml、outerhtml在设置标签之间的内容时,包含的html会被解析;而innertext、outertext则不会;
2) innerhtml、innertext仅设置标签之间的文本,而outerhtml、outertext设置包含自身标签在内文本
以上这篇详谈innerhtml innertext的使用和区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
JavaScript中某一标签内函数用this获取不到当前标签的问题笔记
MSAL.js 2.0发布 支持授权代码流 (authorization code flow)
网友评论