单击链接jquery时更改活动li

时间:2022-11-21 15:32:01

I want to make a menu, and change the class when clicking.

我想制作一个菜单,点击时更改课程。

When i click on the "li" with no class="active", i want jquery to add a class on the empty <li> and remove it from the othes "li".

当我点击没有class =“active”的“li”时,我希望jquery在空

  • 上添加一个类,然后从othes“li”中删除它。

  • <li class="active"><a href="javascript:;" onclick="$.data.load(1);">data</a></li>
    <li><a href="javascript:;" onclick="$.data.load(2);">data 2</a></li>
    

    can somebody help me ? :)

    有人能帮助我吗? :)

    5 个解决方案

    #1


    24  

    I think you mean this:

    我想你的意思是:

    $('li > a').click(function() {
        $('li').removeClass();
        $(this).parent().addClass('active');
    });
    

    #2


    13  

    // When we click on the LI
    $("li").click(function(){
      // If this isn't already active
      if (!$(this).hasClass("active")) {
        // Remove the class from anything that is active
        $("li.active").removeClass("active");
        // And make this active
        $(this).addClass("active");
      }
    });
    

    #3


    4  

    $('li').click(function()
    {
        $('li', $(this).parent()).removeClass('active');
        $(this).addClass('active');
    }
    

    #4


    2  

    $(window).load(function(){
        page=window.location.pathname.split("/").pop();
        menuChildren = $('a[href="' + page + '"]');  
        $(menuChildren).parent('li').addClass('active');
    });
    

    The above code will look up the url and pop out the last element (Which is the file name). Then it finds the anchor tag with href attribute which has the same value of the url then it puts an active class for its parent li tag

    上面的代码将查找url并弹出最后一个元素(这是文件名)。然后它找到具有href属性的锚标记,该标记具有相同的url值,然后它为其父li标记放置一个活动类

    #5


    1  

    This should get you close.

    这应该让你接近。

    $("li").click(function() {
      $("li").removeClass("active");
      $(this).addClass("active");
    });
    

    #1


    24  

    I think you mean this:

    我想你的意思是:

    $('li > a').click(function() {
        $('li').removeClass();
        $(this).parent().addClass('active');
    });
    

    #2


    13  

    // When we click on the LI
    $("li").click(function(){
      // If this isn't already active
      if (!$(this).hasClass("active")) {
        // Remove the class from anything that is active
        $("li.active").removeClass("active");
        // And make this active
        $(this).addClass("active");
      }
    });
    

    #3


    4  

    $('li').click(function()
    {
        $('li', $(this).parent()).removeClass('active');
        $(this).addClass('active');
    }
    

    #4


    2  

    $(window).load(function(){
        page=window.location.pathname.split("/").pop();
        menuChildren = $('a[href="' + page + '"]');  
        $(menuChildren).parent('li').addClass('active');
    });
    

    The above code will look up the url and pop out the last element (Which is the file name). Then it finds the anchor tag with href attribute which has the same value of the url then it puts an active class for its parent li tag

    上面的代码将查找url并弹出最后一个元素(这是文件名)。然后它找到具有href属性的锚标记,该标记具有相同的url值,然后它为其父li标记放置一个活动类

    #5


    1  

    This should get you close.

    这应该让你接近。

    $("li").click(function() {
      $("li").removeClass("active");
      $(this).addClass("active");
    });