gpt4 book ai didi

javascript - 选择子li时如何将父li类设置为active

转载 作者:行者123 更新时间:2023-11-28 17:10:21 26 4
gpt4 key购买 nike

我有以下菜单结构。 When a child li is selected, for example "Overview", I would like to have the parent li highlighted, in this case "About Us".

<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="drop"><a href="#" >Home</a></li>
<li class="drop"><a href="#" >About Us</a>
<ul class="drop-down">
<li class="drop"><a href="#" class="active">Overview</a></li>
<li class="drop"><a href="#" >Security</a></li>
<li class="drop"><a href="#" class="active">Expertise</a></li>
<li class="drop"><a href="#" >Process</a></li>
<li class="drop"><a href="#" >Platform</a></li>
<li class="drop"><a href="c#" >Careers</a></li>
</ul>
</li>
</ul>
</div>

我尝试了以下方法,但没有用:

$(document).ready(function(){
var pathname = window.location.pathname,
page = pathname.split(/[/ ]+/).pop(),
menuItems = $('.navbar-nav');
menuItems.each(function(){
var mi = $(this),
miHrefs = mi.attr("href"),
miParents = mi.parents('li.a');
if(page == miHrefs) {
miParents.addClass("active").siblings().removeClass('active');
}
});
});

最佳答案

我认为这会有帮助,试试这个:

$("li a").on("click", function() {
$(this).parent().parents("li").css("background-color","red");
});

关于javascript - 选择子li时如何将父li类设置为active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29365429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com