js dom的previoussibling 和 nextsibling...
时间:2010-08-11 来源:aidenliu
原来地址:http://blog.csdn.net/lin49940/archive/2009/10/26/4728125.aspx
定义和用法
nextSibling 属性可返回某个元素之后紧跟的节点(处于同一树层级中)。
如果无此节点,则该属性返回 null。
语法:
view plaincopy to clipboardprint?
- nodeObject.nextSibling
previousSibling 属性可返回某节点之前紧跟的节点(处于同一树层级)
如果没有此节点,那么该属性返回 null。
语法:
view plaincopy to clipboardprint?
- nodeObject.previousSibling
从上面的previousSibling 和 nextSibling 的定义可以看出, 这两个属性获取的是节点, 而不是元素.
Internet Explorer 会忽略节点间生成的空白文本节点(例如,换行符号),而 Mozilla 不会这样做。
对于节点的节点类型nodeType
| 节点编号: | 节点名称: |
|---|---|
| 1 | Element |
| 2 | Attribute |
| 3 | Text |
| 4 | CDATA Section |
| 5 | Entity Reference |
| 6 | Entity |
| 7 | Processing Instrucion |
| 8 | Comment |
| 9 | Document |
| 10 | Document Type |
| 11 | Document Fragment |
| 12 | Notation |
从上面的表可以看出, 获取的节点不一定就是元素, 也可以是文本或其他的.
所以, 要获取下个元素或上个元素, 用下面的方法
view plaincopy to clipboardprint?- //check if the next sibling node is an element node
- function get_nextsibling(n)
- {
- var x=n.nextSibling;
- if(x == null) return null;
- while (x && x.nodeType!=1)
- {
- x=x.nextSibling;
- }
- return x;
- }
- //check if the previous sibling node is an element node
- function get_previoussibling(n)
- {
- var x=n.previousSibling;
- if(x == null) return null;
- while (x && x.nodeType!=1)
- {
- x=x.previousSibling;
- }
- return x;
- }
相关阅读 更多 +
排行榜 更多 +










