Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Expressjs将类部分添加到li元素_Javascript_Node.js_Express - Fatal编程技术网

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;
        }
      }
    });