[英]active class with bootstrap sidebar
The sidebar needs to change active class after click.This is my bootstrap code and i tried it with this javascript code but it doesn't works.单击后侧边栏需要更改活动类。这是我的引导代码,我用这个 javascript 代码尝试过,但它不起作用。
$(function(){ $('.sidebar1 a').filter(function(){return this.href==location.href}).parent().addClass('active').siblings().removeClass('active') $('.sidebar1 a').click(function(){ $(this).parent().addClass('active').siblings().removeClass('active') }) })
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div id="sidebar"> <div class="list-group"> <ul class="siderbar1"> <li><a href="HhwController" class="list-group-item active">Hoe het werkt</a></li> <li><a href="OveronsController" class="list-group-item">Over ons</a></li> <li><a href="VpController" class="list-group-item">Veiligheid en privacy</a></li> <li><a href="FAQController" class="list-group-item">FAQ</a></li> <li><a href="Contactform" class="list-group-item">Contact</a></li> </ul> </div> </div>
please try like below.请尝试如下。
<script type="text/javascript">
$(function(){
// $('.sidebar1 a').filter(function(){return this.href==location.href}).parent().addClass('active').siblings().removeClass('active'); i have not tried this line
$('.sidebar1 a').click(function(){
$(".list-group-item").removeClass('active');
$(this).addClass('active');
})
})
</script>
The easiest way how to unset .active class form siblings and set it to current one is如何取消设置 .active 类形式的兄弟姐妹并将其设置为当前的最简单方法是
$('.sidebar1 a').click(function(){
$('.sidebar1 .active').removeClass('active');
$(this).parent().addClass('active');
});
try this尝试这个
$('.sidebar1 a').click(function(){
$('.sidebar1 .active').removeClass('active');
$(this).parent().addClass('active');
});
First you have to remove active
class from all anchor
tags.首先,您必须从所有
anchor
标签中删除active
类。 Then, you can add active
class to the anchor
tag which is clicked.然后,您可以将
active
类添加到单击的anchor
标记中。 Also, you have to use e.preventDefault()
, to stop browser from redirecting.此外,您必须使用
e.preventDefault()
来阻止浏览器重定向。
$('.siderbar1 a').on('click', function(e) { e.preventDefault(); $('.siderbar1 a').removeClass('active'); $(this).addClass('active'); })
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div id="sidebar"> <div class="list-group"> <ul class="siderbar1"> <li><a href="HhwController" class="list-group-item active">Hoe het werkt</a></li> <li><a href="OveronsController" class="list-group-item">Over ons</a></li> <li><a href="VpController" class="list-group-item">Veiligheid en privacy</a></li> <li><a href="FAQController" class="list-group-item">FAQ</a></li> <li><a href="Contactform" class="list-group-item">Contact</a></li> </ul> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.