如何递归 DOM 树?
因此,我有一系列嵌套的 ul 元素作为树的一部分,如下所示:
<ul>
<li>
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
<ul>
<li>2.1</li>
<li>
<ul>
<li>2.2</li>
</ul>
</li>
</ul>
<ul>
<li>3.1</li>
<li>3.2</li>
</ul>
</li>
</ul>
假设当 3.1 是选定节点时,当用户单击上一个节点时,选定节点应该是 2.2。坏消息是,可能有任意数量的深度。如何使用 jquery 找到与当前选定节点相关的前一个节点 (li)?
So I have a series of nested ul elements as part of a tree like below:
<ul>
<li>
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
<ul>
<li>2.1</li>
<li>
<ul>
<li>2.2</li>
</ul>
</li>
</ul>
<ul>
<li>3.1</li>
<li>3.2</li>
</ul>
</li>
</ul>
Let's say when 3.1 is the selected node and when the user clicks previous the selected node should then be 2.2. The bad news is that there could be any number of levels deep. How can I find the previous node (li) in relationship to the currently selected node using jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DOM 定义了文档遍历类,允许顺序处理树内容。 TreeWalker 和 NodeIterator 类是完美的候选者。
首先,我们创建一个 TreeWalker 实例,它可以顺序迭代
节点。
接下来我们迭代到 TreeWalker 中的当前节点。假设我们在
myNode
中引用了当前节点。我们将遍历这个步行器,直到到达myNode
或空值。到达 TreeWalker 中的节点后,获取前一个节点是小菜一碟。
您可以尝试此处的示例。
这是向后树步行器的通用版本。它是 TreeWalker 界面的一个小包装。
它需要一个起始节点和一个自定义过滤器函数来删除不需要的元素。以下是从给定节点开始并且仅包含叶
li
元素的示例用法。使用交互式示例进行了更新。点击列表项可突出显示上一个列表项。
DOM has defined document traversal classes that allow for sequential processing of tree content. The TreeWalker, and NodeIterator classes for perfect candidates for this.
First we create a TreeWalker instance that can sequentially iterate through
<li>
nodes.Next we iterate up to the current node in this TreeWalker. Let's say we had a reference to our current node in
myNode
. We will traverse this walker until we reachmyNode
or a null value.Having reached our node in this TreeWalker, getting the previous node is a piece of cake.
You can try an example here.
Here is a generic version of a backwards tree walker. It's a tiny wrapper around the TreeWalker interface.
It takes a starting node, and a custom filter function to remove unwanted elements. Here is an example usage of starting at a given node, and only including leaf
li
elements.Updated with an interactive example. Tap on a list item to highlight the previous list item.
你可以在 jQuery 中这样做:
http://jsfiddle.net/haP5c/
基本上,如果你点击一个元素,它将搜索前一个没有任何子节点的 LI 节点。如果是,则将其视为链中的前一个。正如您在 jsfiddle 上看到的,它运行得很好。
You can do this like this in jQuery:
http://jsfiddle.net/haP5c/
Basically, if you click an element, it'll search for the previous LI node that doesn't have any children. If it does, it considers it the previous one in the chain. As you can see on the jsfiddle, it works perfectly.
一个相当简单的方法,无需任何 JavaScript 框架即可工作:
现在
prev
保存之前的 LI。编辑:请参阅我的其他答案,了解使用 XPath 的解决方案,该解决方案不存在 treeface 提到的缺陷。
A rather simple approach that would work without any JavaScript-framework:
Now
prev
holds the previous LI.EDIT: Please see my other answer for a solution using XPath that doesn't have the flaws mentioned by treeface.
以下是如何将其作为 jQuery 插件(根据 MIT 许可证授权)来实现。在 jQuery 加载后立即加载它:
查找最后一个没有
li
元素(最外面的ul
具有 idmyUL
) >ul 孩子们,您可以在$(this)
上使用该插件,如下所示:您可以 在 jsFiddle 上尝试一下。
Here's how to do it as a jQuery plug-in (licensed under the MIT License). Load it just after jQuery has loaded:
To find the last previous
li
element (the outermostul
having the idmyUL
) that has noul
children, you could use the plug-in on$(this)
like this:You can try it out on jsFiddle.
我的简单方法存在缺陷,这让我有点“恼火”,这是一个使用 XPath 的解决方案:
注意:我从 Treeface 的答案中复制了事件处理代码,因为我对该框架不太熟悉。
Mildly "annoyed" by the fact that my simple approach had flaws, here's one solution with XPath:
NOTE: I copied the event handling code from treeface's answer as I'm not that familiar with that framework.