当前位置: 首页 > 图文教程 > 网络编程 > Javascript > childNodes.length与children.length的区别

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 childNodes.length与children.length的区别


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 307 ::
收藏到网摘: n/a

childNodes.length与children.length的值常不一样。 因为childNodes包含的不仅仅只有html节点,所有属性文本等都包含在childNodes里面,你可以通过nodeType来判断是哪种类型的节点,只有当nodeType==1时才使元素节点,2是属性节点,3是文本节点。而children.length只计算节点的数量。
包括进行节点长度的计算,节点的移动时都要注意这一点。
要获得一个元素的文本,比如要获得一个P节点的文本,有的人喜欢使用innerHTML,更改也是一样,其实这不太好,因为使用innerHTML浏览器会认为你要插入的东西是一段标签文本,而你只想插入一段纯文本,这就导致浏览器试图去解析你的那段纯文本,这会造成不必要的资源浪费。所以建议使用element.firstChild.nodeValue,为什么是firstChild呢?因为childNodes[0]就是文本节点,不信你可以看看它的nodeType