jquery 下一个兄弟姐妹
我一直在努力解决这个问题,但如果没有一些认真的解决方法,我似乎无法解决这个问题。
如果我有以下 HTML:
<ul>
<li class="parent"> headertext </li>
<li> text </li>
<li> text </li>
<li> text </li>
<li class="parent"> headertext </li>
<li> text </li>
<li> text </li>
</ul>
现在,我现在如何选择第一个父级(或第二个父级)后面的
标记? 基本上选择一个带有 class="parent"
的
我可以使用嵌套列表重组列表,但我不想这样做。 有什么建议么?
I've been trying to get this problem solved, but I can't seem to figure it out without some serious workarounds.
if I have the following HTML:
<ul>
<li class="parent"> headertext </li>
<li> text </li>
<li> text </li>
<li> text </li>
<li class="parent"> headertext </li>
<li> text </li>
<li> text </li>
</ul>
Now, how do I now just select the <li>
tags following the first parent (or second, for that matter)? Basically selecting an <li>
with class="parent"
and the following siblings until it reaches another <li>
with the parent class.
I could restructure the list with nested lists, but I don't want to do that. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实际上,您可以使用 nextUntil() 轻松完成此操作。
无需编写您自己的“nextUntil”,因为它已经存在。
前任。 -
或者按照文森特的建议 -
actually, you can easily do this using nextUntil().
no need to write your own "nextUntil" since it already exists.
ex. -
or as suggested by Vincent -
问题的根源在于,您归类为父级的
实际上不是它们“下面”的
The root of your problem is that the
<li>
s you have classed as parent really are NOT parents of the<li>
s "below" them. They are siblings. jQuery has many, many functions that work with actual parents. I'd suggest fixing your markup, really. It'd be quicker, cleaner, easier to maintain, and more semantically correct than using jQuery to cobble something together.我认为没有办法在不使用每个选择器的情况下做到这一点,因为任何其他选择器也会选择第二个父级及其下一个兄弟姐妹。
I don't think there is a way to do this without using each since any of the other selectors will also select the second parent and it's next siblings.
您必须自己运行循环,因为 jQuery 不知道如何在特定条件下停止。
但你可能更好地使用一个真正结构化的列表来表达你的父母/孩子的关系
You will have to run the loop yourself since jQuery does not know how to stop on a specific condition.
But you may be better using a really structured list for your parent/children relationship
我知道这是一个非常古老的线程,但是 Jquery 1.4 有一个名为 nextUntil 的方法,该方法对于此目的可能很有用:
http://api.jquery.com/nextUntil/
I know this is a very old thread, but Jquery 1.4 has a method called nextUntil, which could be useful for this purpose:
http://api.jquery.com/nextUntil/