开发者

Selecting Parent's Child in jQuery

I think all I need is a selector, but can't find one. I want to select the anchor tag, but can only select the li using .parent()...

jQuery:

$("nav ul li > ul").parent().append('<span class="arrow">&darr;</span>');

HTML:

<li><a href="#">The Events</a>
  <ul>
    <li><a href="#">Schedule</a></li>
   开发者_如何学编程 <li><a href="#">Buy Tickets</a></li>
    <li><a href="#">Features</a></li>
  </ul>
</li>

The code above puts the arrow on the right lis, but one level to high. I need to target the li's a tag.


You can use .siblings(), like this:

$("nav ul li > ul").siblings("a").append('<span class="arrow">&darr;</span>');

You can find a full list of traversal functions here.


Answered myself. Using .children("a") I was able to select the anchor. Final code looked like:

$("nav ul li > ul").parent().children("a").append('<span class="arrow">&darr;</span>');


Does this happen on clicking the link or onload?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜