开发者

Finding which ul list's a link has been clicked using jQuery

开发者 https://www.devze.com 2023-03-01 05:43 出处:网络
I can get the current link being clicked using the following code, which returns a number, telling me the n-th li开发者_StackOverflow社区nk has been clicked:

I can get the current link being clicked using the following code, which returns a number, telling me the n-th li开发者_StackOverflow社区nk has been clicked:

    $("#thumbs ul a").live('click',function(){
        index_image = $("#thumbs ul a").index(this);
    }

However, I want to know which ul list has the clicked link inside of it. I also want this to return a number e.g. the 2nd ul has been clicked. I have been trying the following solutions with little luck:

    //Solution One
    $("#thumbs ul a").live('click',function(){
        index_image = $("#thumbs ul a").index(this);
        $("#thumbs ul a").parent('ul').index(this);
    }

    //Solution Two
    $("#thumbs ul").live('click',function(){
        $("#thumbs ul").index(this);
    }

The HTML is marked up as follows:

    <div id="thumbs">
        <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
       </ul>
        <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
       </ul>
        <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
       </ul>
    </div>


Can you try solution one with this:

//Solution One
$("#thumbs ul a").live('click',function(){
    index_image = $("#thumbs ul a").index(this);
    var parentUl = $(this).closest('ul'); // this will return the ul container object
}

You can then do whatever with the parentUl variable, which is a reference to the ul element you are looking for.

0

精彩评论

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