JS中innerHTML/outerHTML和innerText/outerText以及value 的区别与使用
value
value:value是表单元素特有的属性,输入输出的是字符串
如下面的例子,获取到的是他们的value值
<input type="text" id="inP"> <div id="P">
<p>
5566555
</p>
</div>
<button id="btn" value="123">确定</button> <script>
var inP = document.getElementById("inP");
var P = document.getElementById("P");
var btn =document.getElementById("btn");
btn.onclick=function(){
console.log("===========input============")
console.log("value:=="+inP.value); console.log("===========P================")
console.log("value:=="+P.value);
console.log("===========button================")
console.log("value:=="+btn.value); } </script>
input框输入:123456

最后输出如下:

innerHTML/outerHTML
(1) innerHTML设置或获取标签所包含的HTML+文本信息 (从标签起始位置到终止位置全部内容,包括HTML标签,但不包括自身)
(2)outerHTML设置或获取标签自身及其所包含的HTML+文本信息(包括自身)
如下图所示

来个栗子吧:
<input type="text" id="inP"> <div id="P">
<p>5566555</p>
</div> <button id="btn" value="123">确定</button> <script>
var inP = document.getElementById("inP");
var P = document.getElementById("P");
var btn =document.getElementById("btn"); btn.onclick=function(){
console.log("===========input============") console.log("innerHTML:=="+inP.innerHTML);
console.log("outerHTML:=="+inP.outerHTML); console.log("===========P================") console.log("innerHTML:=="+P.innerHTML);
console.log("outerHTML:=="+P.outerHTML); console.log("===========button================") console.log("innerHTML:=="+btn.innerHTML);
console.log("outerHTML:=="+btn.outerHTML); }
innerText/outerText
(1) innerText设置或获取标签所包含的文本信息(从标签起始位置到终止位置的内容,去除HTML标签,但不包括自身)
innerText 打印标签之间的纯文本信息,会将标签过滤掉,低版本的火狐浏览器不支持,而是支持textContent
(2) outerText设置或获取标签自身及其所包含的文本信息(包括自身)
innerText和outerText在获取的时候是相同效果,
但在设置时,innerText仅设置标签所包含的文本,而outerText设置包含包括标签自身在内的文本
<input type="text" id="inP"> <div id="P"><p>5566555</p></div> <button id="btn" value="123">确定</button> <script>
var inP = document.getElementById("inP");
var P = document.getElementById("P"); var btn =document.getElementById("btn");
btn.onclick=function(){
console.log("===========input============")
console.log("innerText:=="+inP.innerText);
console.log("outerText:=="+inP.outerText); console.log("===========P================")
console.log("innerText:=="+P.innerText);
console.log("outerText:=="+P.outerText);
console.log("===========button================")
console.log("innerText:=="+btn.innerText);
console.log("outerText:=="+btn.outerText);
}

如上图红框所示,innerText和outerText获取值得时候结果是一样的。
inner HTML和innerText的对比
innerText 打印标签之间的纯文本信息
innerHtml 打印标签之间的内容,包括标签和文本信息
<div id="P"><p>5566555</p></div> <button id="btn" value="123">确定</button> <script>
var inP = document.getElementById("inP");
var P = document.getElementById("P"); var btn =document.getElementById("btn");
btn.onclick=function(){
console.log("===========P================")
console.log("innerHTML:=="+P.innerHTML);
console.log("innerText:=="+P.innerText); }

如果将P标签的内容更改如下

inner HTML也会按照原格式进行输出;
结果如下:

尊重原创,转载请注明出处 https://home.cnblogs.com/u/lshdashi/
如有不当之处,请留言指出,谢谢!
JS中innerHTML/outerHTML和innerText/outerText以及value 的区别与使用的更多相关文章
- JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?jQuery中的text()、html()和val()
一.JS中innerHTML.outerHTML.innerText .outerText.value的区别与联系?jS中设置或者获取所选内容的值:①innerHTML :属性设置或返回该标签内的HT ...
- js中innerHTML、outerHTML与innerText的用法与区别
____________________________________________________________________________________________________ ...
- JS中innerHTML,innerText,value
一·.JS初学者易混淆的问题:innerHTML,innerText,value(他们和JQ的区别:JS→value,JQ→value()) 1.getElementById("a" ...
- (转)JS中innerHTML,innerText,value
原文:http://holysonll.blog.163.com/blog/static/21413909320134111054352/ JS中innerHTML,innerText,value 2 ...
- JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?
1.innerHTML 属性 (参考自<JavaScript高级程序设计>294页) 在读模式下,innerHTML 属性返回与调用元素的所有子节点(包括元素.注释和文本节点)对应的 HT ...
- js中innerHTML与innerText的用法与区别
用法: <div id="test"> <span style="color:red">test1</span> te ...
- js中innerHTML和innerText的用法
<div id="test"> <span style="color:red">test1</span> test2 < ...
- js中,var 修饰变量名和不修饰的区别
js中 允许在定义变量的时候 不加var 修饰符.js会在当前作用域下寻找上下文是否定义了此变量, 如果没有找到则会为这个变量分配内存.当且将其视为window的成员. 也就是全局变量. 如果加了va ...
- js中return、return true、return false的区别
一.返回控制与函数结果, 语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制, 无函数结果,语法为:return; 在大多数情况下,为事件 ...
随机推荐
- PythonGIS可视化—Matplot basemap工具箱
原文链接:http://www.douban.com/group/topic/32821988/ 原文链接:http://www.cnblogs.com/vamei/archive/2012/09/1 ...
- 知识工程.Vs.软件构架,框架,设计模式.
软件工程-原文链接:http://tech.it168.com/a2009/0902/672/000000672853.shtml 此文章详细给出了软件设计的基本概念和用途,文章链接:http://w ...
- 杭电2053 WA
#include<stdio.h> int main() { ]; while(scanf("%d",&n)!=EOF) { ;i<=;i++) { a[ ...
- JdbcTemplate 增删查改
准备工作:1,拷贝工具类JDBCUtils, 2,导入包commons-logging-1.2-javadoc.jar spring-beans-5.0.0.RELEASE-javadoc.jar ...
- Aeroplane chess HDU - 4405_数学期望_逆推
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...
- 路飞学城Python-Day150
最近由于在忙别的事情,所以路飞的课程就往后延期了,感觉很难受,不过我还是依然坚持学下去,必须的 最近在忙的事情 1.进入了数据分析行业,需要学习更多的知识 2.开始对数据收集负责,写各种爬虫 3.对数 ...
- MySQL 单表查询多表查询
一 单表查询 表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...
- C++进阶 STL(3) 第三天 函数对象适配器、常用遍历算法、常用排序算法、常用算数生成算法、常用集合算法、 distance_逆序遍历_修改容器元素
01昨天课程回顾 02函数对象适配器 函数适配器是用来让一个函数对象表现出另外一种类型的函数对象的特征.因为,许多情况下,我们所持有的函数对象或普通函数的参数个数或是返回值类型并不是我们想要的,这时候 ...
- BA-冷源系统--一次流量控制方案(转载)
空调水变一次流量控制方案 浙大网新快威科技 黄逸林 一.引言 建筑物中央空调系统的冷冻水一次泵,传统上都采用固定转速水泵.空调水的变一次流量控制系统(VPF:Variable-Primary-Flow ...
- 带有public static void main方法的类,其中的成员变量必须是static的,否则main方法没法调用。除非是main里的局部变量。因为main方法就是static的啊。
带有public static void main方法的类,其中的成员变量必须是static的,否则main方法没法调用.除非是main里的局部变量.因为main方法就是static的啊.
