html--innerHTML用法及和与innerText区别

一、innerHTML用法

innerHTML在JS是双向功能:获取对象的内容 或 向对象插入内容;
如:<div id="aa">这是内容</div>

我们可以通过 document.getElementById(‘aa’).innerHTML 来获取id为aa的对象的内嵌内容;
也可以对某对象插入内容,如 document.getElementById(‘abc’).innerHTML=’这是被插入的内容’;
这样就能向id为abc的对象插入内容。
实例:

1.获取段落p的 innerHTML(html内容)
 <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <script type="text/javascript">
            function getInnerHTML(){
                alert(document.getElementById("test").innerHTML);
            }
        </script>
    </head>
    <body>
        <p id="test"><font color="#000">嗨豆壳 www.hi-docs.com</font></p>
        <input type="button" onclick="getInnerHTML()" value="点击" />
    </body>
 </html>
2.设置段落p的 innerHTML(html内容)
<html>
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
            <script type="text/javascript">
            function setInnerHTML(){
                document.getElementById("test").innerHTML = "<strong>设置标签的html内容</strong>";
            }
            </script>
            </head>
        <body>
            <p id="test"><font color="#000">嗨豆壳 www.hi-docs.com</font></p>
            <input type="button" onclick="setInnerHTML()" value="点击" />
        </body>
  </html>
二、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浏览器打开,弹出内容为 <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

通过上面两个示例,可以看出:

  • 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标签之后的内容,这是个在所有浏览器均可使用的方法。

  • 10
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值