我在嵌套菜单上添加类时遇到问题。当我点击带有类get-view的链接时,我需要在包含div的所有链接中添加一个类,其中包含类导航项,包括我单击的链接。我完全迷失了如何选择包含div并将类应用于内部的所有链接。任何帮助都非常感激。
我的HTML看起来像这样
<div class="nav-item">
<a href="1.html" class="get-view subnav-main"><span>Main Link 1</span></a>
</div>
<div class="nav-item">
<a href="2.html" class="get-view subnav-main"><span>Main Link 2</span></a>
</div>
<div class="nav-item">
<div class="subnav-main subnav-more">
<a href="3.html" class="get-view subnav-main"><span>Main Link 3</span></a>
<div class="subnav-subnav subnav-closed">
<a href="3.html" class="get-view">Sublink 1</a><br />
<a href="4.html" class="get-view">Sublink 2</a><br />
<a href="5.html" class="get-view">Sublink 3</a><br />
</div>
</div>
</div>
答案 0 :(得分:3)
这应该有效:
$(".get-view").click(function() {
// Remove [YourClass] from any element that might already have it
$(".YourClass").removeClass("YourClass");
//Add [YourClass] based on your requirements
$(this).closest(".nav-item").find("a").addClass("YourClass");
});