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的更多相关文章

  1. JavaScript中childNodes和children的区别

    我在学习JavaScript对DOM操作的过程中,发现了使用childNodes属性,得不到我想要的结果,因此我就从JavaScript高级程序设计中了解了childNodes和children的区别 ...

  2. Javascript 中childNodes和children的区别

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. JavaScript中childNodes、children、nodeValue、nodeType、parentNode、nextSibling详细讲解

    其中属性.元素(标签).文本都属于节点 <title></title> <scripttype="text/javascript"> windo ...

  4. javascript中childNodes与children的区别

    1.childNodes:获取节点,不同浏览器表现不同: IE:只获取元素节点: 非IE:获取元素节点与文本节点: 解决方案:if(childNode.nodeName=="#text&qu ...

  5. js表单验证处理和childNodes 和children 的区别

    一.对提交表单进行空值验证 html代码: <form action="#"onsubmit="return validate_form(this);" ...

  6. parentNode、parentElement,childNodes、children 它们有什么区别呢?

    parentNode.parentElement,childNodes.children 它们有什么区别呢?parentElement 获取对象层次中的父对象. parentNode 获取文档层次中的 ...

  7. js下firstElementChild firstChild 以及childNodes和children方法

    一. <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementB ...

  8. childNodes和children

    childNodes 返回指定元素的子节点集合,包括HTML节点,所有文本(元素之间的空格换行childNodes会看作文本节点). 通过nodeType来判断节点的类型: 元素 1 属性 2 文本 ...

  9. Javascript DOM基础(二) childNodes、children

    childNodes知识点: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Typ ...

随机推荐

  1. File类_深度遍历文件夹_练习

    遍历指定目录下的所有文件和文件夹 import java.io.File; public class FileTest { public static void main(String[] args) ...

  2. MessageQueue 相关概念

    /**  * Implements a thread-local storage, that is, a variable for which each thread  * has its own v ...

  3. KVM虚拟化图

  4. Arduino IDE for ESP8266 项目(3)创建AP+STA

    官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html STA (客户端)手机连接路由器 S1 *简 ...

  5. esp8266(2) 智能配置

    http://www.arduino.cn/thread-46594-1-1.html http://blog.csdn.net/sadshen/article/details/47049129 ht ...

  6. 2017-2018-2 20155314《网络对抗技术》Exp5 MSF基础应用

    2017-2018-2 20155314<网络对抗技术>Exp5 MSF基础应用 目录 实验内容 实验环境 基础问题回答 预备知识 实验步骤--基于Armitage的MSF自动化漏洞攻击实 ...

  7. oracle 索引的几种方式

    一.查询索引的高度 select index_name,blevel,leaf_blocks,num_rows,distinct_keys,clustering_factorfrom user_ind ...

  8. JavaScript高级程序设计学习(六)之设计模式

    每种编程语言都有其自己的设计模式.不禁让人疑惑设计模式是用来做什么?有什么用? 简单的说,设计模式是为了让代码更简洁,更优雅,更完美. 同时设计模式也会让软件的性能更好,同时也会让程序员们更轻松.设计 ...

  9. 浅淡个人学习嵌入式Linux过程

    我专业是电子信息工程,在初入大学的时候,我们的班主任便要我们多多去了解一些关于电子方面的知识.后来我了解到了嵌入式,继而了解到了嵌入式Linux.其实我们学习linux差不多就学习linux内核,但是 ...

  10. 排列数与For的关系

    目录 什么是排列数 用现实模型表示 用Python编程表示 用数学符号表示 规律 规律1 规律2 如果m < n 会怎样? 排列数的应用场景 什么是排列数 排列指将一个集合里的每个元素不重复的排 ...