Jquery Can';t单击子li后,将活动类添加到父li选项卡

Jquery Can';t单击子li后,将活动类添加到父li选项卡,jquery,html,css,Jquery,Html,Css,基本上,我希望如果选择了我的子选项卡中的1,那么子选项卡和父选项卡都将获得活动类 css 试试这个: .dropdown:first-child a.active{ background: #672783; } .dropdown:nth-child(2) a.active{ background: #ff6100; } 演示: $(“.dropdown>.sub_navigation>li>a”)。单击(函数(){ $(this).parents('li.dropdown').

基本上,我希望如果选择了我的子选项卡中的1,那么子选项卡和父选项卡都将获得活动类

css

试试这个:

.dropdown:first-child a.active{
background: #672783;
}

.dropdown:nth-child(2) a.active{
    background: #ff6100;
}

演示:

$(“.dropdown>.sub_navigation>li>a”)。单击(函数(){
$(this).parents('li.dropdown').sides('li').removeClass(“active”);
$(this.parents('li.dropdown').addClass(“active”);//不工作
});
。下拉列表:第一个子a.active{
背景:#672783;
}
.下拉列表:第n个子(2)a.活动{
背景:#ff6100;
}
.主动{
背景:红色;
}

试试这个:

$(".dropdown > .sub_navigation > li > a").click(function(){
       $(this).parents('li.dropdown').siblings('li').removeClass("active");
       $(this).parents('li.dropdown').addClass("active"); 

});
演示:

$(“.dropdown.sub_导航li a”)。单击(函数(){
$(.active”).removeClass('active');
$(this).closest('.dropdown').find('a:first').addClass(“active”);
$(此).addClass(“活动”);
});
.active{
背景色:红色;
}


您是否尝试过使用.parents('.dropdown')而不是.closest()?如果孩子没有活动类,它就不起作用,您可以这样做吗?即使它没有活动类,当您单击“都将被添加”时,它也会被添加?我想您想要的是“如果子导航li a中有活动类,我如何将.active类添加到我的父li?”。有关将活动类添加到子类和父类,请参见我的更新答案。您想将活动类添加到
li
child的标记中吗?实际上我想添加到标记中。。我一直有麻烦。。我认为我的css有问题,似乎无法选择正确的选择器并添加…;-)很高兴我能帮忙!
.dropdown:first-child a.active{
background: #672783;
}

.dropdown:nth-child(2) a.active{
    background: #ff6100;
}
$(".dropdown .sub_navigation li a").click(function(){
    $(".active").removeClass("active");

    var $dropDown =$(this).closest('.dropdown');
    $dropDown.find("a:first").addClass("active");      

    $(this).addClass("active");  
});
$(".dropdown > .sub_navigation > li > a").click(function(){
       $(this).parents('li.dropdown').siblings('li').removeClass("active");
       $(this).parents('li.dropdown').addClass("active"); 

});
$(".dropdown .sub_navigation li a").click(function(){
    $(".active").removeClass('active');
    $(this).closest('.dropdown').find('a:first').addClass("active");
    $(this).addClass("active");
});