jQuery:父母还是兄弟姐妹?以及如何“指导”兄弟姐妹搜索?
我有一个如下所示的列表:
<li class = "current_parent level-0">
<a> Parent Link </a>
<ul class= "children">
<li class = "level-1">
<a> Link to child </a>
</li>
<li class = "level-1 current_page_item">
<a> Link to child </a>
</li>
<li class = "level-1">
<a> Link to child </a>
</li>
</ul>
</li>
基本上,我想从 current_page_item 获取元素的 HREF 向上一层和向下一层。
我可以获得 current_page_item > 的 URL通过 .attr() 很容易,即 $('.current_page_item').attr("href");。
但是我怎样才能告诉 jQuery 向上查找一级呢? .parent() 似乎不对,因为他们不是父母,他们是兄弟姐妹,对吧?
但我怎样才能告诉兄弟姐妹向上或向下移动列表呢?
基本上我想要 $('.current_page_item > ONE LI > A UP').attr("href");和 $('.current_page_item > ONE LI > A DOWN').attr("href");。
再次感谢您对我的 jQuery n00bquest 的帮助!
I have a list that looks like this:
<li class = "current_parent level-0">
<a> Parent Link </a>
<ul class= "children">
<li class = "level-1">
<a> Link to child </a>
</li>
<li class = "level-1 current_page_item">
<a> Link to child </a>
</li>
<li class = "level-1">
<a> Link to child </a>
</li>
</ul>
</li>
Basically, I want to get the HREF of the element one level UP and one level DOWN from the current_page_item .
I can get the URL of the current_page_item > a easily enough via .attr(), i.e. $('.current_page_item').attr("href");.
But how can I tell jQuery to look one level up? .parent() doesn't seem right, because they're not parents, they're siblings,right?
But how can I tell siblings to move UP or DOWN a list?
Basically I want $('.current_page_item > ONE LI > A UP').attr("href"); and $('.current_page_item > ONE LI > A DOWN').attr("href");.
Again, thanks for the help on my jQuery n00bquest!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从
开始,请使用
.next()
和.prev()
获取下一个/上一个兄弟姐妹,例如:您可以在 jQuery API 的树遍历部分。
If you'r starting with the
<li>
use.next()
and.prev()
to get the next/previous siblings, for example:You can find a complete functions to move around like this as well as their descriptions under the Tree Traversal section of the jQuery API.
使用 .prev() 而不是 .parent() (.prev() 和 .next() 都指同级)
use .prev() instead of .parent() (.prev() and .next() both refers to siblings)