反向走DOM树? [英] Walk DOM Tree in Reverse?

查看:52
本文介绍了反向走DOM树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

走向前进的DOM树非常简单但是我不能确定
找出一个很好的干净方式来反向走DOM树。检查

previousSibling是不够的,因为previousSibling可能是一个

节点,它有childNodes,因此''true''previousSibling

将是上一个兄弟的*最深* lastChild ...


例如,给定此图:


1 myAnchor nodeType 1

2 myAnchorText1 nodeType 3

3 myAnchorText2 nodeType 3

4 myBold nodeType 1

5 myText1 nodeType 3

6 myText2 nodeType 3

我希望能够从位置6开始反向行走树

以便我得到:


myText2

myText1

myBold

myAnchorText2

myAnchorText1

myAnchor


有什么想法吗?


谢谢,

悬崖。

It''s quite simple to walk to the DOM tree going forward however I can''t
figure out a nice clean way to walk the DOM tree in reverse. Checking
previousSibling is not sufficient as the previousSibling could be a
node which has childNodes and therefore the ''true'' previousSibling
would be the *deepest* lastChild of the previousSibling...

For example, given this graph:

1 myAnchor nodeType 1
2 myAnchorText1 nodeType 3
3 myAnchorText2 nodeType 3
4 myBold nodeType 1
5 myText1 nodeType 3
6 myText2 nodeType 3

I want to be able to walk the tree in reverse starting from position 6
so that I get:

myText2
myText1
myBold
myAnchorText2
myAnchorText1
myAnchor

Any ideas?

Thanks,
Cliff.

推荐答案


de ****** *@gmail.com 写道:
向前走到DOM树很简单但是我不能找到一个很好的干净方式来反向行走DOM树。检查previousSibling是不够的,因为previousSibling可能是一个有childNodes的节点,因此''true''previousSibling
将是previousSibling的最深* lastChild ...

例如,给定此图:

1 myAnchor nodeType 1
2 myAnchorText1 nodeType 3
3 myAnchorText2 nodeType 3
4 myBold nodeType 1
5 myText1 nodeType 3
6 myText2 nodeType 3
我希望能够从位置6开始反向走树,这样我才能得到:

myText2
myText1
myBold
myAnchorText2
myAnchorText1
myAnchor

任何想法?
It''s quite simple to walk to the DOM tree going forward however I can''t
figure out a nice clean way to walk the DOM tree in reverse. Checking
previousSibling is not sufficient as the previousSibling could be a
node which has childNodes and therefore the ''true'' previousSibling
would be the *deepest* lastChild of the previousSibling...

For example, given this graph:

1 myAnchor nodeType 1
2 myAnchorText1 nodeType 3
3 myAnchorText2 nodeType 3
4 myBold nodeType 1
5 myText1 nodeType 3
6 myText2 nodeType 3

I want to be able to walk the tree in reverse starting from position 6
so that I get:

myText2
myText1
myBold
myAnchorText2
myAnchorText1
myAnchor

Any ideas?



我可能会错过你的问题: - 实际的HTML代码会有所帮助。

树是......嗯......一棵树。这意味着可以为很多孩子提供b $ b(叶子),但每个孩子只有一个父母(分支)。或者 - 使用

数据库术语 - 这些是一对多关系,而不是多对多关系。



I might be missing your problem:- an actual HTML code would be helpful.
The tree is...uhm... a tree. It means that can be a lot of children
(leaves) but only one parent (branch) for each child. Or - using
database terms - these are one-to-many relations, not many-to-many.


< a href =mailto:de ******* @ gmail.com> de ******* @ gmail.com 写道:
de*******@gmail.com wrote:
走到前面的DOM树很简单但是我不能找到一个很好的干净方式来反向走DOM树。检查
previousSibling是不够的,因为previousSibling可能是一个有childNodes的节点,因此''true''previousSibling
将是上一个兄弟的*最深* lastChild ...


您必须递归上一个兄弟的子树,直到

没有更多的子节点,即直到没有lastChild。

例如,给定此图:

1 myAnchor nodeType 1
2 myAnchorText1 nodeType 3
3 myAnchorText2 nodeType 3
4 myBold nodeType 1
5 myText1 nodeType 3
6 myText2 nodeType 3
我希望能够从位置6开始反向走树,以便得到:
myText2
myText1
myBold
myAnchorText2
myAnchorText1
myAnchor

任何想法?
It''s quite simple to walk to the DOM tree going forward however I can''t
figure out a nice clean way to walk the DOM tree in reverse. Checking
previousSibling is not sufficient as the previousSibling could be a
node which has childNodes and therefore the ''true'' previousSibling
would be the *deepest* lastChild of the previousSibling...
You have to recurse into the subtree of the previousSibling until
there are no more child nodes, i.e. until there is no lastChild.
For example, given this graph:

1 myAnchor nodeType 1
2 myAnchorText1 nodeType 3
3 myAnchorText2 nodeType 3
4 myBold nodeType 1
5 myText1 nodeType 3
6 myText2 nodeType 3

I want to be able to walk the tree in reverse starting from position 6
so that I get:

myText2
myText1
myBold
myAnchorText2
myAnchorText1
myAnchor

Any ideas?




myText2.previousSibling .... parentNode.previousSibl ing.lastChild ...

..previousSibling .... parentNode ...

见?

PointedEars



myText2.previousSibling....parentNode.previousSibl ing.lastChild...
..previousSibling....parentNode...

See?
PointedEars


de ******* @ gmail.com 写道:
de*******@gmail.com writes:
前进到DOM树很简单但是我不能找到一个很好的干净方式来反向行走DOM树。
It''s quite simple to walk to the DOM tree going forward however I can''t
figure out a nice clean way to walk the DOM tree in reverse.




你想要一个深度优先从右到左的遍历节点。

这听起来像是递归的工作:


---

函数traverseDFRTL(元素,动作) {

for(var child = element.lastChild;儿童; child = child.previousSibling){

traverseDFRTL(child,action);

}

action(element);

}

---


来电

---

traverseDFRTL(someElement, function(node){

alert([node.tagName,node.nodeType]);

});

---

会按照所需的顺序提醒节点。

如果你不想要递归,你也可以迭代地进行,但它是

永远不会那么简单。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''



You want a depth-first right-to-left traversal of the nodes.
That sounds like a job for recursion:

---
function traverseDFRTL(element, action) {
for(var child = element.lastChild; child; child = child.previousSibling) {
traverseDFRTL(child, action);
}
action(element);
}
---

Calling
---
traverseDFRTL(someElement, function(node) {
alert([node.tagName, node.nodeType]);
});
---
will alert the nodes in the desired order.
If you don''t want recursion, you can do it iteratively as well, but it
will never be as simple.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


这篇关于反向走DOM树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆