2013-02-01 12 views
0

我想添加一個類到menuepoint如果選擇了一個路徑。location.pathname

這將是容易的,如果每一個網站是自己的.php/.html文件,但是一切都在一個.php文件統治,一切都在導航操作(?action=main?action=userinformation)。

我在找一種替代方式以獲得此作品的路徑的動作。

if (location.pathname == "/index.php") { 
    $("#main1").addClass("mainActive"); 
} else if (location.pathname == "/index.php?action=other") { 
    $("#main2").addClass("mainActive"); 
} 
+1

使用PHP從查詢字符串中讀取'action',然後在適當的元素上設置類。 –

+0

'location.search'包含GET params字符串。 – pawel

回答

0

location.pathname不包括查詢字符串。但你可以將它與location.search結合:

var path = location.pathname + location.search; 
//   "/index.php" + "?action=other" 

if(path === "/index.php?action=other") { 
    // ... 
} 
+0

謝謝,對我很好!非常感謝你。 –

2

也許你可以用like;

var s = location.search; 
if (s === "" || s === "?" || s.indexOf("?action=main") > -1) 
    // main active 
else if (s.indexOf("?action=userinformation") > -1) 
    // userinformation active 
// ... and so on 
相關問題