开发者

jQuery: Parent or Sibling? And how to 'instruct' a sibling search?

开发者 https://www.devze.com 2023-01-20 22:43 出处:网络
I have a list that looks like this: <li class = \"current_parent level-0\"> <a> Parent Link </a>

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?

开发者_StackOverflow中文版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!


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消