jQuery:父母还是兄弟姐妹?以及如何“指导”兄弟姐妹搜索?

发布于 2024-09-28 13:38:21 字数 929 浏览 0 评论 0原文

我有一个如下所示的列表:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2024-10-05 13:38:21

如果您从

  • 开始,请使用 .next().prev() 获取下一个/上一个兄弟姐妹,例如:
  • var prevHref = $(".current_page_item").prev().find("a").attr("href");
    var nextHref = $(".current_page_item").next().find("a").attr("href");
    

    您可以在 jQuery API 的树遍历部分

    If you'r starting with the <li> use .next() and .prev() to get the next/previous siblings, for example:

    var prevHref = $(".current_page_item").prev().find("a").attr("href");
    var nextHref = $(".current_page_item").next().find("a").attr("href");
    

    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.

    时光倒影 2024-10-05 13:38:21

    使用 .prev() 而不是 .parent() (.prev() 和 .next() 都指同级)

    use .prev() instead of .parent() (.prev() and .next() both refers to siblings)

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文