2014-04-25 170 views
0

我正在一個網站上工作,並且我有幾個菜單鏈接。我想突出顯示基於url的當前菜單項。目前我在我的JQuery中有以下代碼:JQuery突出顯示當前菜單項

$(document).ready(function() { 
     debugger; 
     $('#menu a').each(function (index) { 
      if (this.href.trim() == window.location.href) 
       $(this).addClass("current"); 
     }); 
    }); 

只有當url中沒有查詢字符串時,它才能正常工作。當我將一些查詢字符串傳遞給url時,這個JQuery不起作用。

+0

'$(本).attr( 「HREF」)== window.location.href'? –

+1

嗨,@LeeTaylor我試過你的建議,它向我展示了一個錯誤,如** JavaScript運行時錯誤:'href'是undefined ** –

回答

2

如果你不想查詢字符串,你想要location.pathname

這使用filter()只返回相關鏈接(S)...

$("#menu a").filter(function (index) { 
    return this.href.search(location.pathname) !== -1; 
}).addClass("current"); 
+0

嗨@Archer,我需要知道如何做到這一點 –

+0

我修改了答案。嘗試一下,讓我們知道你的方式。 – Archer

+0

謝謝,@Archer ...它工作了我...非常感謝 –