JavaScript使用childNodes和children
childNodes用来获取一个元素的所有子元素,这个包括元素节点和文本节点。
children用来获取一个元素的子元素节点,注意只是元素节点
其中DOM中常见的三种节点分别如下:
元素节点:<body>,<p>,<a>,<div>,<head>.....等等这些标签,都是元素节点
属性节点:title,value,href,id,class等等这些标签的属性,都是属性节点
文本节点:文本节点是包含在在标签之内的内容(双标签)比如<p>this is text node</p>,中间的文字就是文本节点;注意包含在标签中间的不一定是文本节点,比如<div><p></p></div>当中的<p>是元素节点
所以在使用childNodes来获取到一个元素的子元素集合,这是一个数组。
其中最常用的是通过childNodes获得的对象数组中,第一个子元素对象(也即下标为0),这个元素是text,即文本节点,也可以使用firstChild来替代childNodes[0],然后通过nodeValue属性来获取text的值。
如果在标签之中只有一个文本节点,而没有其他元素节点时,也可以使用innerHTML来获取这个节点的文本内容。
具体的用法如下:
<body>
<div id="test">
this is test
<p>this is other test</p>
</div>
</body>
<script>
var div = document.getElementById("test"); alert(div.childNodes.length); //显示3 alert(div.childNodes[0]); //[object Text] alert(div.childNodes[0].nodeValue); //显示this is test
//等价
alert(div.firstChild.nodeValue); //显示this is test alert(div.childNodes[1]); //[object HTMLParagraphElement] alert(div.childNodes[1].childNodes[0].nodeValue); //this is other test
//等价
alert(div.childNodes[1].firstChild.nodeValue); //this is other test
//p标签中无其他节点,可以使用innderHTML
alert(div.childNodes[1].innerHTML); ////this is other test </script>
JavaScript使用childNodes和children的更多相关文章
- JavaScript中childNodes和children的区别
我在学习JavaScript对DOM操作的过程中,发现了使用childNodes属性,得不到我想要的结果,因此我就从JavaScript高级程序设计中了解了childNodes和children的区别 ...
- Javascript 中childNodes和children的区别
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- JavaScript中childNodes、children、nodeValue、nodeType、parentNode、nextSibling详细讲解
其中属性.元素(标签).文本都属于节点 <title></title> <scripttype="text/javascript"> windo ...
- javascript中childNodes与children的区别
1.childNodes:获取节点,不同浏览器表现不同: IE:只获取元素节点: 非IE:获取元素节点与文本节点: 解决方案:if(childNode.nodeName=="#text&qu ...
- js表单验证处理和childNodes 和children 的区别
一.对提交表单进行空值验证 html代码: <form action="#"onsubmit="return validate_form(this);" ...
- parentNode、parentElement,childNodes、children 它们有什么区别呢?
parentNode.parentElement,childNodes.children 它们有什么区别呢?parentElement 获取对象层次中的父对象. parentNode 获取文档层次中的 ...
- js下firstElementChild firstChild 以及childNodes和children方法
一. <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementB ...
- childNodes和children
childNodes 返回指定元素的子节点集合,包括HTML节点,所有文本(元素之间的空格换行childNodes会看作文本节点). 通过nodeType来判断节点的类型: 元素 1 属性 2 文本 ...
- Javascript DOM基础(二) childNodes、children
childNodes知识点: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Typ ...
随机推荐
- File类_常见的方法(获取系统根目录与指定目录的容量)
获取系统根目录 import java.io.File; public class File_ListRoots { public static void main(String[] args) { ...
- Linter pylint is not installed
问题 Linter 'pylint' is not installed. Please install it or select another linter". Error: Module ...
- 使用hint优化Oracle的运行计划 以及 SQL Tune Advisor的使用
背景: 某表忽然出现查询很缓慢的情况.cost 100+ 秒以上:严重影响生产. 原SQL: explain plan for select * from ( select ID id,RET_NO ...
- Oracle 11gR2使用RMAN duplicate复制数据库
11g的RMAN duplicate 个人感觉比10g的先进了很多,10g需在rman备份的基础上进行复制,使用RMAN duplicate创建一个数据完全相同但DBID不同的数据库.而11g的RMA ...
- B类——Stas and the Queue at the Buffet
http://codeforces.com/contest/1151/problem/D 题意: n个学生,每个学生都有自己的位置,最后要使
- 关于ELK
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/getting-started.html 日志: https://ww ...
- C#以管理员用户打开某个程序
static void Main(string[] args) { string path = @"C:\Windows\AppPatch\AppLoc.exe"; Process ...
- 安装Debian后做的一些事情
1.source.list # aliyun deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib deb http: ...
- React-使用Redux-thunk中间件实现ajax数据请求
把异步函数放在生命周期函数里写,生命周期函数会变得越来越复杂,组件会变得越来越大.Redux默认只处理同步,借助redux-thunk ,可以把异步请求放在actionCreators.js里管理,而 ...
- 学习Angularjs向数据库添加数据
今天学习angularjs向数据库添加数据. 学习此篇,得从以往几篇开始,因为那还有创建数据表等演示. 现在来创建一个添加的存储过程: SET ANSI_NULLS ON GO SET QUOTED_ ...