Javascript Expressjs将类部分添加到li元素
我有这个菜单的一部分:Javascript Expressjs将类部分添加到li元素,javascript,node.js,express,Javascript,Node.js,Express,我有这个菜单的一部分: <ul id="submenu"> <li><a href="/mypage/profile/<%=user[0].id %>">Profile</a></li> <li><a href="/mypage/test/<%=user[0].id %>">Test</a><
<ul id="submenu">
<li><a href="/mypage/profile/<%=user[0].id %>">Profile</a></li>
<li><a href="/mypage/test/<%=user[0].id %>">Test</a></li>
</ul>
如何根据单击的链接向li元素添加一类选定的
,
。对于本例,您不需要任何服务器端的东西,您可以通过简单的jQuery代码实现效果(请记住之前在页面中包括jQuery):
$(函数(){
//把可能的页面标题放在这里
//它们也必须在url中
变量可能的页面=['profile','test'],
路径=window.location.pathname,
模式,页面,i,len;
对于(i=0,len=mably_pages.length;我的意思是如何将类添加到您当前所在的页面上?是的,完全正确,因此如果我在配置文件页面上,我希望添加例如class=“selected”。例如
。而path.test不是一个函数OK我修复了它,我很匆忙,抱歉:)它应该是模式测试(path)
当然可以。谢谢。效果很好!
app.get('/mypage/profile/:id', function(req,res,next) {
res.render('site/mypage/cv', {
title: 'profile'
});
});
app.get('/mypage/test/:id', function(req,res,next) {
res.render('site/mypage/cv', {
title: 'test'
});
});
$(function() {
// put the possible page titles here
// they must be in the url also
var possible_pages = ['profile', 'test'],
path = window.location.pathname,
pattern, page, i, len;
for (i = 0, len = possible_pages.length; i <= len; i++) {
page = possible_pages[i];
pattern = new RegExp(page);
if (pattern.test(path)) {
$('.submenu .' + page).addClass('selected');
break;
}
}
});