点击子li
中的链接后,我尝试将活动课程添加到家长li
。我有下拉菜单,在我的下拉菜单中,HTML代码在以下网站中:
http://www.dawntravels.com/
我尝试过这段代码却没有成功:
<script>
$(".arrow_carrot-2right").click(function (e) {
e.preventDefault();
$(this).closest('li').addClass('selected'); // I also tried .parent().addClass
});
</script>
答案 0 :(得分:1)
HTML:
<ul class="arrow_carrot-2right">
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
JQuery:
$('.arrow_carrot-2right > li').click(function () {
$(this).addClass('selected').siblings().removeClass('selected');
});
CSS:
.selected{
background-color:green;
}
演示:
答案 1 :(得分:0)
由于您的课程arrow_carrot-2right
已应用于<i>
而不是anchor tag
,请尝试此操作,
$('ul.sf-menu li a').on('click',function(e){
e.preventDefault();
$(this).closest('li').addClass('selected');
});
答案 2 :(得分:0)
尝试:
//get the current URL
var url = window.location;
//after the link has been clicked and page loaded,
//find the clicked anchor tag using the loaded page name
$('.sf-menu a').filter(function() {
return this.href == url;
//add the 'active' class to the outermost parent <li> of the matched <a> tag
}).parents("li").addClass('active');