如何跳到第一个李的锚?

时间:2014-11-17 04:03:45

标签: jquery

这是html代码:

<span class="expander"><span class="caret"></span></span>
<ul class="expander-target displaynone">
     <li><a href="http://localhost:8008/management/ecshop/edit">aaaaaaaa</a></li>
     <li><a href="http://localhost:8008/management/coupon">bbbbbbb</a></li>
     <li><a href="http://localhost:8008/management/sale">ccccccc</a></li>
     <li><a href="http://localhost:8008/management/mail/template">ddddddd</a></li>
</ul>

以下是jquery代码:

   $('.expander').click(function(){
    $(this).toggleClass('open');
    $(this).nextAll('.expander-target').slideToggle('fast');
                   //console.log($(this).siblings("ul li:first-child").attr("href");  
  });

如何编写代码跳转到第一个li锚地址?感谢

2 个答案:

答案 0 :(得分:1)

只需使用此

console.log($(".expander-target li:first  a").attr("href")); 

此处正在运作fiddle

<强>更新

要使用this使用此功能,它会在点击的项目中找到下一个ul

console.log($(this).next("ul.expander-target").find('li:first a').attr("href"));

答案 1 :(得分:0)

请尝试:

$( document ).ready(function() {
    alert($(".expander-target li:first-child a").attr("href"));
    alert($("ul li:first-child a").attr("href"));
 });

API details here