我在 Html 导航栏中有一个下拉菜单。当使用 Css 选择子项时,我想让下拉标题处于事件状态。
家 关于我们
在这里,我希望在使用 jquery 和 css 选择我们的团队时突出显示关于我们。
我的 Jquery:
$("nav li a").each(function () {
if (str.indexOf(this.href.toLowerCase()) > -1) {
$("li.active").removeClass("active");
//alert($(this).parent().attr("Id"));
$(this).parent().addClass("active");
}
// else {
// $("li.active").removeClass("active");
//}
});
最佳答案
我不太清楚你为什么使用 each() 但是 既然你说选择了。假设下拉标题在点击事件后激活是安全的吗? 如果是这样,那么您可以这样做。
$(".nav li > a").click(function () {
if($(this).attr('role')){
$(this).parents('li.dropdown').addClass('active').siblings().removeClass('active');
}else{
$(this).parent().addClass('active').siblings().removeClass('active');
}
});
如果不是那么Crispy George's答案可能就是您要找的那个
关于jquery - 使 Html 菜单下拉菜单处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26987961/