当前位置: 首页 > 图文教程 > 网络编程 > Javascript > DOM 脚本编程中的兄弟节点

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 中的 DOM 脚本编程中的兄弟节点


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 349 ::
收藏到网摘: n/a

兄弟节点之间可以通过 previousSibling 和 nextSibling 属性访问同一级别上的不同子节点。这个兄弟节点是元素还是文本节点在现代浏览器上运行是怎么样的呢? 除IE外的浏览器是将换行符作为内容的文本节点(nodeType为3)。而元素的话,nodeType为1。下面是查找它们的实用方法:
复制代码 代码如下:

lastSibling:function(node){
var tempObj = node.parentNode.lastChild;
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null)
{
tempObj=tempObj.previousSibling;
}
return (tempObj.nodeType==1)?tempObj:false;
}

这是《深入浅出JavaScript》书中DOMhelp库中lastSibling方法的源码。与 mootools 库中实现源码差不多:
复制代码 代码如下:

'last-child': function(){
var element = this;
while ((element = element.nextSibling)){
if (element.nodeType == 1) return false;
}
return true;
}

这是在 Mootools 1.2.4 源码中的 last-child() 方法。