我的一个项目中有一个Bootstrap 4导航栏。有几个链接。单击它们会导致其对应的页面。悬停效果也有效。唯一的问题是" Home"选项卡保持活动状态,其他选项卡在单击并转到相应页面时不会变为活动状态。
代码:
bsnavbar.handlebars(部分)
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/scry">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/scry/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="scry/report">Report an issue</a>
</li>
</ul>
</div>
</nav>
head.handlebars(部分)
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
main.handlebars(布局)
<!DOCTYPE html>
<html lang="en">
<head>
{{>head}}
<title>{{title}}</title>
</head>
<body>
<div class="container">
{{>bsnavbar}}
<hr>
</div>
<div class="container">
{{{body}}}
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
router.js(重要的是)
const express = require("express");
const router = express.Router();
router.get("/", function(req, res){
res.status(200);
res.header("Content-Type", "text/html");
res.render("index");
});
router.get("/about", function(req, res){
res.status(200);
res.header("Content-Type", "text/html");
res.render("about")
});
router.get("/report", function(req, res){
res.status(200);
res.header("Content-Type", "text/html");
res.render("report");
});
router.use(function(req, res){
res.status(404);
res.header("Content-Type", "text/html");
res.render("404");
});
module.exports = router;