innerHTML和innerText
document 对象中有innerHTML和innerText 两个属性, 这两个属性都是获取document对象的文本内容的,这两个属性间有哪些区别呢?通过几个例子来看一下。
示例1
- <html>
- <head><title>innerHTML</title></head>
- <body>
- <p id="p1">hello world </p>
- <script>
- var content = document.getElementById("p1");
- alert(content.innerHTML);
- alert(content.innerText)
- </script>
- </body>
- </html>
通过IE浏览器打开,弹出内容为 "hello world" 和 "hello world"
通过 Firefox 浏览器打开,弹出内容为 "hello world" 和 "undefined"
通过 chrome 浏览器打开,弹出内容为 "hello world" 和 "hello world"
示例2
- <html>
- <head><title>innerHTML</title></head>
- <body>
- <div id="d1"><p id="p1">hello world </p></div>
- <script>
- var content = document.getElementById("d1");
- alert(content.innerHTML);
- alert(content.innerText)
- </script>
- </body>
- </html>
通过IE浏览器打开,弹出内容为 和 通过 Firefox 浏览器打开,弹出内容为 和 通过 chrome 浏览器打开,弹出内容为 和
通过上面两个示例,可以看出:
innerHTML指的是从对象的起始位置到终止位置的全部内容,包括Html标签。
innerText 指的是从起始位置到终止位置的内容,但它去除Html标签。
同时,innerHTML 是所有浏览器都支持的,innerText
是IE浏览器和chrome 浏览器支持的,Firefox浏览器不支持。其实,innerHTML 是W3C 组织规定的属性;而innerText
属性是IE浏览器自己的属性,不过后来的浏览器部分实现这个属性罢了。
outerHTML
说到innerHTML,顺便说一下跟innerHTML相对的outerHTML属性。
继续看上面的代码,将alert(content.innerText) 修改为 alert(content.outerHTML)
通过浏览器可以看到弹出框为<p id="p1">hello world </p>
和 <divid="d1"><p id="p1">hello world</p></div>
outerHTML指的是除了包含innerHTML的全部内容外, 还包含对象标签本身。
总结说明
innerHTML是符合W3C标准的
属性,而innerText只适用于IE浏览器(现在也适应chrome浏览器),因此,尽可能地去使用
innerHTML,而少用innerText,如果要输出不含HTML标签的内容,可以使用innerHTML取得包含HTML标签的内容后,再用正则
表达式去除HTML标签,下面是一个简单的符合W3C标准的示例:
- <html>
- <head><title>innerHTML</title></head>
- <body>
- <div id="d1"><p id="p1">hello world </p></div>
- <script>
- var content = document.getElementById("p1");
- alert(content.innerHTML.replace(/& lt;.+?>/gim,''));
- </script>
- </body>
- </html>
弹出的为去掉了html标签之后的内容,这是个在所有浏览器均可使用的方法。
innerHTML和innerText的更多相关文章
- innerHTML与innerText的异同
在一道面试题中看到的. 1.功能讲解: innerHTML 设置或获取位于对象起始和结束标签内的 HTML outerHTML 设置或获取对象及其内容的 HTML 形式 innerText 设置或获取 ...
- JS中innerHTML,innerText,value
一·.JS初学者易混淆的问题:innerHTML,innerText,value(他们和JQ的区别:JS→value,JQ→value()) 1.getElementById("a" ...
- JS魔法堂:被玩坏的innerHTML、innerText、textContent和value属性
一.前言 由于innerText并非W3C标准属性,因此我们无法在FireFox中使用它(修正:FF45+已经支持innerText属性),一般情况下我们可以使用textContent来代替,但它两者 ...
- JS中innerHTML 和innerText和value的区别
(1)innerHTML 和innerText和value的区别: innerHTML innerText是对非表单元素进行操作的. value是对表单元素进行操作的. (2)innerHTML 和i ...
- (转)JS中innerHTML,innerText,value
原文:http://holysonll.blog.163.com/blog/static/21413909320134111054352/ JS中innerHTML,innerText,value 2 ...
- (转)innerHTML、innerText和outerHTML、outerText的区别
原文:http://walsh.iteye.com/blog/261966 innerHTML.innerText和outerHTML.outerText的区别 博客分类: CSS/ ...
- innerHTML与innerText的PK
一.innerText属性用来定义对象所要输出的文本,在本例中innerText把对象it中的文本"您喜欢看微微一笑很倾城吗?"变成了"超级喜欢!"(语句it. ...
- textContent、innerHTML、innerText、outerText、outerHTML、nodeValue使用场景和区别
今天要讲的这些属性都可以用来获取某个元素的内容,你可能会觉得不可思议,或是说上一句"丧心病狂"也.但当你看完以下内容后,会发现除outerText无用外,其他的都有各自的使用场景, ...
- 测试开发技术:DOM中 innerHTML、innerText、outerHTML、outerText的区别
测试开发技术:DOM中 innerHTML.innerText.outerHTML.outerText的区别 我们在做web自动化的过程中通过dom处理web页面元素,那么你就要了解innerHT ...
随机推荐
- C的快速趋向实验 -->
今天刚学到C的一个新玩法,非常有意思,叫趋向于,写作“-->”,比如说如果要实现一个倒数的程序,我们可以定义一个变量 counter,然后让它趋向于0... #include <stdio ...
- 理解Python的迭代器
首先,廖雪峰老师的教程中解释了迭代器和生成器,这篇文章只是补充和我个人的总结. 什么是迭代 可以直接作用于for循环的对象统称为可迭代对象(Iterable). 可以被next()函数调用并不断返回下 ...
- 矩阵分解(rank decomposition)文章代码汇总
矩阵分解(rank decomposition)文章代码汇总 矩阵分解(rank decomposition) 本文收集了现有矩阵分解的几乎所有算法和应用,原文链接:https://sites.goo ...
- ubuntu 64bit “arm-linux-gcc: No such file or directory”问题的解决方法
安装lsb-core sudo apt-get install lsb-core
- *[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...
- 如何在C++中使用WebService
gsoap主页 http://sourceforge.net/projects/gsoap2 使用gsoap生成所需的WebService 下载后的gsoap包为:(点击到我的资源中下载) 将他解 ...
- 第一回写的用arraylist模拟栈操作
package hashMap; import java.util.ArrayList; import d.Student; /** * 用ArrayList模拟栈操作 * @author zhuji ...
- 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication 基于语句和行的复制的优势和劣势
17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication 基于语句和行的复制的优势和劣势 每 ...
- c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制
using mshtml;using System;using System.Collections.Generic;using System.Linq;using System.Security.P ...
- System.Windows.Forms中的Message Structure
结构用途说明Implements a Windows message. Properties 1.public IntPtr HWnd { get; set; } Gets or sets the w ...