因此,我使用Twitter Bootstrap 3來構建我的前端,在本例中爲簡單的選項卡式菜單。爲了使它工作,我掙扎了很多,因爲菜單鏈接在點擊它們時不想正確突出顯示。我的意思是,如果你例如點擊about.php這個頁面,該鏈接應該被標記爲活動的,因此應用了一些CSS樣式。我在stackowerflow上看到很多帖子,但他們只爲我部分工作。例如,某些人發佈的jQuery代碼會做適當的突出顯示,但會阻止鏈接正常工作。使用分頁時Bootstrap活動菜單突出顯示中斷
最後,我發現一個解決方案,除了當我使用分頁時工作得很好。讓我來解釋:
這裏是我的菜單引導的html代碼:
<nav class="row">
<ul class="nav nav-tabs nav-justified">
<li><a href="index.php"> Home </a></li>
<li><a href="about.php"> About Us </a></li>
<li><a href="contact.php"> Contact Us </a></li>
<li><a href="services.php"> Our Services</a></li>
<li><a href="portfolio.php"> Portfolio </a></li>
</ul>
<nav>
爲了使菜單高亮的工作我用這javaScrpt代碼:
var url = window.location;
// Will only work if string in href matches with location
$('ul.nav a[href="' + url + '"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.nav a').filter(function() {
return this.href == url;
}).parent().addClass('active').parent().parent().addClass('active');
所以,當我開始應用程序index.php被標記爲活動,這很好,如果我點擊about.php,我會去那個頁面,它也被標記爲活動,這也很棒。但是因爲我在index.php上分頁,並且我點擊分頁鏈接2 url會變成:index.php?page = 2,突出顯示會中斷,index.php不會被標記爲活動狀態。
有人知道這裏發生了什麼,我們能做些什麼來解決這個問題?我對JS不太擅長。
這可能會幫助您正確http://stackoverflow.com/questions/6644654/parse-url-with-jquery解析URL -javascript – Charles380
我不知道如何可以幫助我... Oo – offline