我有一个带有语言的bootstrap 4导航栏,当我选择语言时,请使用javascript更改url和按钮文本。
当我选择法语时,将所有页面网址更改为“ / fr / about”“ / fr / contact”
如何更改网址,无论何时单击导航项,如果选择了about us
,请将网址更改为/fr/about
,还更改下拉菜单href
//header.ejs
<nav>
<div class="dropdown">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown mr-4">
<a class="nav-link dropdown-toggle" href="/en/contact" id="navbarDropdownProvider" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
about us
</a>
</li>
<li class="nav-item dropdown mr-4">
<a class="nav-link dropdown-toggle" href="/en/contact" id="navbarDropdownProvider" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
contact us
</a>
</li>
</ul>
<button id="language" class="btn btn-warning dropdown-toggle" type="button" id="dropdownMenu2"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="clickButton()">
English
</button>
<div id="languagelist" class="dropdown-menu" aria-labelledby="dropdownMenu2" onclick="clickItem(); return false">
<a class="dropdown-item" href="/en">English</a>
<a class="dropdown-item" href="/fr">French</a>
</div>
</div>
</nav>
//index.ejs
<%- include header.ejs %>
<h1>Welcome to My Site</h1>
//app.js
var express = require('express');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs')
var languageRouter = require('./routes/language');
app.use('/:lan', languageRouter);
app.use('/:lan/contact', contactusRouter);
app.use('/:lan/aboutus', aboutusRouter);
//language.js
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index.ejs'});
});
module.exports = router;
//aboutus.js
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('aboutus.ejs');
});
module.exports = router;