Jquery - 选择直接 li 文本而不选择子 ul 文本
我试图将嵌套 html 无序列表的结构作为数据库来组织和分析一些需要的信息。我正在尝试使用 jQuery 过滤、计数和呈现信息。我努力使列表不具有任何 class 或 id 属性,以便它们非常干净。只有根才会有这样的类或 id:
<ul id="root">
<li> First first-level element
<ul>
<li> First second-level element
<ul>
<li>First Third-level element</li>
<li>Second Third-level element</li>
</ul>
</li>
<li> Second second-level element
<ul>
<li>Third Third-level element</li>
<li>Fourth Third-level element</li>
</ul>
</li>
</ul>
</li>
<li> Second first-level element
<ul>
<li> Third second-level element
<ul>
<li>Fifth Third-level element</li>
</ul>
</li>
</ul>
</li>
</ul>
我的问题是:如何选择 li 的直接子文本节点而不选择该 li 的子级和孙级 ul(即其子列表)中的文本?例如,给定上面的 html 列表,我希望能够列出第二级中的所有文本节点:
- 第一个第二级元素
- 第二个第二级元素
- 第三个第二级元素
或者来自第三级...等等。这将允许我列出并计算给定级别中的项目。我最接近的是:
// to select items in second but not third level
$('ul#root ul').not('ul#root ul ul').children('li')
但这不是一个灵活的解决方案。如果列表有很多级别(比如七个)怎么办?要选择第六级,您必须执行以下操作:
// to select items in second but not third level
$('ul#root ul ul ul ul ul').not('ul#root ul ul ul ul ul ul').children('li')
一定有另一种方法,但我还没有找到。非常感谢任何建议。
I'm trying to take a structure of nested html unordered lists as a database for some information a need to organize and analyze. I'm trying to filter, count and present the information using jQuery. I'm striving for the lists not to have any class or id attribute, so that they are very clean. Only the root would have a class or id like this:
<ul id="root">
<li> First first-level element
<ul>
<li> First second-level element
<ul>
<li>First Third-level element</li>
<li>Second Third-level element</li>
</ul>
</li>
<li> Second second-level element
<ul>
<li>Third Third-level element</li>
<li>Fourth Third-level element</li>
</ul>
</li>
</ul>
</li>
<li> Second first-level element
<ul>
<li> Third second-level element
<ul>
<li>Fifth Third-level element</li>
</ul>
</li>
</ul>
</li>
</ul>
My question is: how can I select a li's immediate child text node without selecting text in that li's child and grandchild ul's (i.e. its sublists)? For example, given the html list above, I would like to be able to come up with a list of all text nodes in the second level:
- First second-level element
- Second second-level element
- Third second-level element
Or all text from the third level... etc. That would allow me to list and count the items in a given level. The closest I've been is:
// to select items in second but not third level
$('ul#root ul').not('ul#root ul ul').children('li')
But it is not a flexible solution. What if the list has many levels, say seven? to select sixth level you would have to do something like:
// to select items in second but not third level
$('ul#root ul ul ul ul ul').not('ul#root ul ul ul ul ul ul').children('li')
There must be another way but I haven't found it. Any suggestions are much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如此处所述:
jQuery:查找包含嵌套 ul 的列表项的文本
“普通 DOM 方法允许仅访问一个元素的文本内容”
所以这是一种解决方案:这将一一输出第三级元素:
这输出第二级元素:
这输出第一级元素:
As stated here:
jQuery: Find the text of a list item that contains a nested ul
"Normal DOM methods allow to access text contents for just one element"
So this is one solution: This outputs third level elements one by one:
This outputs second level elements:
This outputs first level elements:
如果您想选择第二个 li,例如
;第二个二级元素
,您可以将>
子选择器与eq
一起使用,如下所示:对于第三个,您设置
eq
到2
因为它的索引从0
开始。要循环它们,您可以使用
each
,如下所示:更多信息:
If you want to select second li eg
<li> Second second-level element
, you can use the>
child selector witheq
like this:For the third you set
eq
to2
because its indexing starts from0
.To loop over them, you can use the
each
like this:More Info:
更稳健的方法是使用遍历“内容”并选择类型为 (3) 的节点,该节点是 HTML 中文本子元素的节点类型。例如:
A more robust method is to use the traversal "contents" and select node with type of (3) which is the node type for a text child element in HTML. For example: