2011-12-13 62 views
0

說我有在部分菜單:Expressjs添加類li元素在部分

<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> 

這些路由:

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

}); 

我怎麼能一類selected添加到li元素取決於點擊哪個鏈接?

+0

你的意思是你怎麼添加一個類到您目前正確的頁面? – alessioalex

+0

是的,所以如果即時消息在個人資料頁面上,我想添加例如class =「selected」.Like'

  • Profile
  • '。 – georgesamper

    回答

    0

    在這個例子中,你不需要任何服務器端的東西,就可以實現簡單的jQuery代碼的效果(記得之前包括jQuery的頁面):

    $(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; 
        } 
        } 
    }); 
    
    +0

    和path.test不是一個函數 – georgesamper

    +0

    好吧我修好了,我很着急抱歉:)它當然應該是'pattern.test(path)'。謝謝。很棒! – georgesamper