我是HTML和JavaScript的新手,现在我遇到了一个问题,我认为你们很多人一直在与自己打交道。
我有一个导航栏,默认情况下,当我点击不同的页面参考时,它不会更改活动状态。
' Home' nav-item默认是活动的,我想在单击其他导航项时更改活动状态。但我不知道该怎么办?据我所知,这并不是太多代码,但我很乐意在这里得到一些帮助。
这是我的代码:
<head>
<meta charset="UTF-8">
<title>MaU Movie database</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://bootswatch.com/4/cyborg/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<link rel="icon" href="mumdblogo.png">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="index.html" id="movielogo"><img src="moviefylogo.png"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor02">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.html">Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="library.html">Library</a>
</li>
<li class="nav-item">
<a class="nav-link" href="uploads.html">Uploads</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h3 class="text-center">Search Movie!</h3>
<form id="searchForm">
<input type="text" class="form-control" id="searchText" placeholder="Search for any movie...">
</form>
</div>
</div>
<div class="container">
<div id="movies" class="row">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="main.js"></script>
</body>
</html>
答案 0 :(得分:4)
您可以在代码的末尾执行此操作。在<script src="main.js"></script>
<script>
$(document).ready(function() {
$( ".mr-auto .nav-item" ).bind( "click", function(event) {
event.preventDefault();
var clickedItem = $( this );
$( ".mr-auto .nav-item" ).each( function() {
$( this ).removeClass( "active" );
});
clickedItem.addClass( "active" );
});
});
</script>
而不是通过链接(event.preventDefault()
)跟随他们,例如。 about.html或uploads.html此snipet代码更改了单击项目的活动类。
编辑1:
对于静态页面(或由例如php提供),你不需要任何aditional js代码,但是你应该调整导航到下面的例子(看看活动&#39;类是什么,sr-only span current是可选的,但很好地放在例子中。
index.html(您的基本代码)
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.html">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="library.html">Library</a>
</li>
<li class="nav-item">
<a class="nav-link" href="uploads.html">Uploads</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
</ul>
<强> library.html 强>
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="library.html">Library<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="uploads.html">Uploads</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
</ul>
<强> uploads.html 强>
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="library.html">Library</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="uploads.html">Uploads<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
</ul>
<强> about.html 强>
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="library.html">Library</a>
</li>
<li class="nav-item">
<a class="nav-link" href="uploads.html">Uploads</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="about.html">About<span class="sr-only">(current)</span></a>
</li>
</ul>
答案 1 :(得分:0)
我认为最简单的解决方案是检查窗口pathname
对象中的location
并相应地更改nabber状态。
这样的事情会起作用:
var pathname = window.location.pathname,
pages = ['home', 'library', 'uploads','about'];
$('.nav-item').each(function(i) {
if (pathname.includes(pages[i])) this.addClass('active');
else if (this.className.includes('active')) this.removeClass('active');
});