[英]How can I active link in Navigation Bar when I click any item located in that bar?
I have a problem about changing the class attribute as active when I click any selected item in the navigation bar.当我单击导航栏中的任何选定项目时,我遇到了关于将 class 属性更改为活动状态的问题。
I wrote this js code which is shown below to implement this process but it didn't work.我编写了如下所示的这段 js 代码来实现这个过程,但它不起作用。
Here is my HeaderPartialView part.这是我的HeaderPartialView部分。
.... css files
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Here is my _Layout.cshtml part.这是我的_Layout.cshtml部分。
....
@{
Html.RenderAction("HeaderPartial", "Home");
}
@{
Html.RenderAction("NavbarPartial", "Home");
}
....
@{
Html.RenderAction("JSFilesPartial", "Home");
}
Here is my JSFilesPartial files.这是我的JSFilesPartial文件。
<script src="~/Content/SiteLayout/assets/js/navigation-bar.js"></script>
Here is my navigation-bar.js file.这是我的navigation-bar.js文件。
$(document).ready(function () {
var current = location.pathname;
$('.navigation-bar li a').each(function(){
var $this = $(this);
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
});
Here is NavbarPartial file.这是NavbarPartial文件。
<nav id="navbar" class="navbar">
<ul class="navigation-bar">
<li><a class="active" href="/Home/Index">Home</a></li>
<li><a href="/Home/About Us">About Us</a></li>
<li><a href="/Home/Projects">Projects</a></li>
</ul>
<i class="bi bi-list mobile-nav-toggle"></i>
</nav><!-- .navbar -->
How can I fix the issue?我该如何解决这个问题?
$(document).ready(() => { $(".somClass li a").on('click', (e) => { $(".somClass li a").removeClass('active'); $(e.target).addClass('active'); }); });
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="somClass"> <li><a class="active" href="#">link 1</a></li> <li><a href="#">link 2</a></li> <li><a href="#">link 3</a></li> </ul>
Since you have active class as prop of anchor element you should try with .navigation-bar li a
instead由于您有活动的 class 作为锚元素的道具,因此您应该尝试使用.navigation-bar li a
You're adding the active
class on the li
element.您正在li
元素上添加active
的 class 。 It should be adding to the a
element instead.它应该添加到a
元素中。 The following should work:以下应该有效:
<script type="text/javascript">
$(document).ready(function () {
$(".navigation-bar li a").on("click", function () {
$(".navigation-bar li a").removeClass("active");
$(this).addClass("active");
});
});
</script>
But it will fail because you're switching pages, and the javascript is reloaded every time you click in a anchor element.但它会失败,因为您正在切换页面,并且每次单击锚元素时都会重新加载 javascript。
I think this is what you're looking for:我想这就是你要找的:
var current = location.pathname;
$('.navigation-bar li a').each(function(){
var $this = $(this);
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
It basically searches with navigation element is currently active, adding the active
class on the a
element.它基本上使用当前活动的导航元素进行搜索,在a
元素上添加active
的 class。
Here is the solution:这是解决方案:
Navbar导航栏
<nav id="navbar" class="navbar">
<ul class="navigation-bar">
<li><a href="/Home/Index">AnaSayfa</a></li>
<li><a href="/Home/Hakkimizda">Hakkımızda</a></li>
<li><a href="/Home/Hizmetler">Hizmetler</a></li>
<li><a href="/Content/SiteLayout/portfolio.html">Portfolio</a></li>
<li><a href="/Content/SiteLayout/blog.html">Blog</a></li>
<li><a href="/Home/BizKimiz">Biz Kimiz</a></li>
<li><a href="/Home/Iletisim">Bize Ulaşın</a></li>
</ul>
<i class="bi bi-list mobile-nav-toggle"></i>
</nav><!-- .navbar -->
Js file js文件
$(document).ready(function () {
var current = location.pathname;
$('.navigation-bar li a').each(function () {
var $this = $(this);
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.