分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

innerHTML&innerHTML与innerText的区别&outerHTML&innerHTML与html的区别

发布时间:2023-09-06 01:31责任编辑:顾先生关键词:HTML

 innerHTML 

innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML。

语法:HTMLElementObject.innerHTML=text

所有主要浏览器都支持 innerHTML 属性

innerHTML在JS是双向功能:获取对象的内容或向对象插入内容;

innerHTML 属性用于设置或返回指定标签之间的 HTML 内容。 ?语法 ????????Object.innerHTML = "HTML";// 设置 ?????var html = Object.innerHTML;// 获取 ????例子 1 ????获取段落p的 innerHTML(html内容) ????????<html> ?????<head> ?????<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> ?????<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>

document 对象中有innerHTML和innerText 两个属性, 这两个属性都是获取document对象的文本内容的,这两个属性间有哪些区别呢?通过几个例子来看一下。

<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"

<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浏览器自己的属性,不过后来的浏览器部分实现这个属性罢了。

说到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() 与html( )的区别

在一个 HTML 文档中, 我们可以使用 .html() 方法来获取任意一个元素的内容。 如果选择器匹配多于一个的元素,那么只有第一个匹配元素的 HTML 内容会被获取。

innerHTML 是从对象的起始位置到终止位置的全部内容,包括Html标签

innerHTML&innerHTML与innerText的区别&outerHTML&innerHTML与html的区别

原文地址:http://www.cnblogs.com/flyingLcode/p/8053498.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved