jQuery “active” class assignment

前端 未结 3 1831
我在风中等你
我在风中等你 2021-01-02 21:16

All I am trying to accomplish is to be able to have an unordered list of links in which one is clicked, the parent list item is assigned the class \"active.\" Once another l

3条回答
  •  天命终不由人
    2021-01-02 21:55

    Something like the following ought to do it

    $(function() {
        $('li a').click(function(e) {
            e.preventDefault();
            var $this = $(this);
            $this.closest('ul').children('li').removeClass('active');
            $this.parent().addClass('active');
        });
    });
    

    Working Demo

提交回复
热议问题