在一个网站中,我正在尝试使此特定的navbar活动类正常工作。当我单击当前页面时,我希望链接变为蓝色。由于我是新手,因此我们将不胜感激。提前致谢。
这是导航栏:
<div class="nav-scroller py-1 mb-2">
<nav class="nav d-flex justify-content-between">
<a class="active p-2 text-muted" href="#"><b> Business</b> </a>
<a class="p-2 text-muted" href="#"><b> Design </b></a>
<a class="p-2 text-muted" href="#"><b>Travel </b></a>
<a class="p-2 text-muted" href="#"><b> Opinion </b></a>
</nav>
</div>
这是JavaScript代码:
$('.nav .p-2 .text-muted').click(function(){
$('.nav .p-2 .text-muted').removeClass('active');
$(this).addClass('active');
})
我还使用CSS将当前导航栏页面链接变为蓝色:
.nav > .p-2.activate {
color:blue;
}
答案 0 :(得分:0)
在您的JQuery
中添加了名为active
的类,但在css
中,您拥有了类activated
。将其更改为active
.nav > .p-2.active { color:blue; }
答案 1 :(得分:0)
首先,您在选择selector
时犯了错误,请添加
$('.nav .p-2.text-muted').click(function(){
$('.nav .p-2.text-muted').removeClass('active');
$(this).addClass('active');
})
和css
.nav > .p-2.active {
color:blue;
}
答案 2 :(得分:0)
这是工作示例:
$(document).ready(function(){
$('nav a').click(function(){
$('nav a').removeClass('active');
$(this).addClass('active');
let link=$(this).attr('href');
window.location.href=link;
});
});
.nav >a.active>b{
color:blue;
}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<div class="nav-scroller py-1 mb-2">
<nav class="nav d-flex justify-content-between">
<a class="active p-2 text-muted" href="https://fonts.google.com"><b> Business</b> </a>
<a class="p-2 text-muted" href="https://facebook.com"><b> Design </b></a>
<a class="p-2 text-muted" href="google.com"><b>Travel </b></a>
<a class="p-2 text-muted" href="#"><b> Opinion </b></a>
</nav>
</div>
</body>
</html>
您需要将section="yourpagename"
从服务器端发送到前端端
现在您可以通过2种方式显示活动链接:
1)第一个是三元条件并在标记中添加类
类似的东西:
<a class="p-2 text-muted <% section=="Business"?'active':'' %>" href="#"><b> Business</b> </a>
2)第二种方法是使用jQuery添加活动类
<a id="Business" class="p-2 text-muted" href="#"><b> Business</b> </a>
$(document).ready(function(){
var section= "Business" . <--send value from server side same as link id
$('#'+section).addClass('active');
});