2012-05-08 69 views
1

我在我的网站的每个页面上包含的header.php页面中都有一个导航栏。如何向当前页面添加一个current_page类?如何在导航栏中动态突出显示当前页面,比如Stackoverflow

我试图

$('a[href="'+window.location+'"]').addClass('menu-highlight'); 

但如果我的GET变量设置不工作,我的HREF的是相对的URL。

我有一个想法是在PHP页面上包含文件来保存对应于该页面的数字,并用Jquery在该属性中更改该数字。

最佳做法是什么?

回答

1

最好的做法是在服务器端代码中处理这个问题。但是,如果你使用绝对路径链接,而不是完整的URL链接,您可以使用

window.location.pathname

排除查询字符串,而不是

window.location.href

,其中包括它

window.location.href 
=> "http://stackoverflow.com/questions/10506079/how-to-dynamically-highlight-the-current-page-in-nav-bar-like-stackoverflow?test=foo" 

window.location.pathname 
=> "https://stackoverflow.com/questions/10506079/how-to-dynamically-highlight-the-current-page-in-nav-bar-like-stackoverflow" 
相关问题