我有下面的菜單,我想將類active
添加到<li>
,其中包含用戶當前正在查看的頁面的鏈接。向包含當前頁面的菜單鏈接的li添加類
<ul class="sidebar-menu">
<li class="treeview"><a href="#"><i class="fa fa-dashboard"></i> <span>Text</span></a></li>
<li class="treeview"><a href="#"><i class="fa fa-th"></i><span>Subtext 1</span><i class="fa fa-angle-left pull-right"></i></a>
<ul class=" treeview-menu">
<li><a href="#"><i class="fa fa-circle-o"></i> Text 1</a></li>
<li><a href="#"><i class="fa fa-circle-o"></i> Text 2</a></li>
</ul>
</li>
</ul>
我想這jQuery代碼,但它不爲我工作:
$(function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
// now grab every link from the navigation
$('.treeview a').each(function(){
// and test its normalized href against the url pathname regexp
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active treeview-menu');
}
});
});
向我們展示'url'變量的值是什麼? – Manwal
我對Java不太好,我需要做功能菜單。如果你能給我一個可行的代碼,那麼你對我的所有讚賞。 – AdrianEWS
@AdrianEWS java與javascript有什麼關係 – madalinivascu