开发者

How to find out the previously selected navigation link in jQuery?

开发者 https://www.devze.com 2023-01-21 04:08 出处:网络
I have a navigation set up using ULs, LIs, and As. The hightlighted/selected tab\'s anchor has a class of selected. When a new nav link is clicked, how can I find out what the ID of the previously sel

I have a navigation set up using ULs, LIs, and As. The hightlighted/selected tab's anchor has a class of selected. When a new nav link is clicked, how can I find out what the ID of the previously sele开发者_JAVA百科cted anchor's tab is?


try this:

html

<ul>
    <li><a href="#">link1</a></li>
    <li><a href="#">link2</a></li>
</ul>

js

$("a").click(function() {
   var $current = $(this);
   var $prev = $current.closest("ul").find("a.selected");
    if($prev.length) {
       alert($prev.text())   
       $prev.removeClass("selected");
    }
    $current.addClass("selected");
    return false;
});


myId = $('a.selected').attr('id')
0

精彩评论

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