代码之家  ›  专栏  ›  技术社区  ›  Cofey

如何在jQuery中找到之前选择的导航链接?

  •  1
  • Cofey  · 技术社区  · 14 年前

    2 回复  |  直到 14 年前
        1
  •  1
  •   Mouhannad    14 年前

    试试这个:

    html格式

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

    $("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;
    });
    
        2
  •  0
  •   Diodeus - James MacFarlane    14 年前
    myId = $('a.selected').attr('id')