2011-08-04 23 views
0

我的JavaScript函數看起來像這樣:如何改用location.href location.pathname的

 function markActiveLink() { 

      var path = location.pathname; 
      var home = "/"; 

      if (path == home) 
       return 
      $("a[href='" + [path || home] + "']").parents("li").each(function() { 
       $(this).removeClass('menu_item'); 
       $(this).addClass("menu_item_active"); 
      }); 
     } 

但我想用document.location.href而不是location.pathname找到鏈接。我試過只是改變它,但然後功能根本不工作 - >我的鏈接都沒有被選中。

我的一些鏈接的代碼看起來像這樣:

<ul> 
      <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserOrder.aspx?id=<%=pe.UserId%>&type=NMO"> 
       <%=Me.GetLocalResourceObject("NMOrders.Text")%> 
      </a></li> 
      <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserOrder.aspx?id=<%=pe.UserId%>&type=MO"> 
       <%=Me.GetLocalResourceObject("MOrders.Text")%> 
      </a></li> 
      <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserPage.aspx?id=<%=pe.UserId%>"> 
       <%=Me.GetLocalResourceObject("UserPage.Text")%> 
      </a></li> 
     </ul> 

和頁面上的那些鏈接源看起來像這樣:

<ul> 
       <li><a href="/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=NMO"> 
        User Orders NMO 
       </a></li> 
       <li><a href="/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO"> 
        User Orders MO 
       </a></li> 
       <li><a href="/App/User/UserPage.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1"> 
        User Page 
       </a></li> 
</ul> 

而且隨着location.pathname的這些鏈接valuse將只有/App/User/UserOrder.aspx我需要檢查整個鏈接。這就是爲什麼我試圖使用location.href來代替。

location.href是例如:http://localhost/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO 然後location.pathname是:/App/User/UserOrder.aspx

任何這裏幫助非常感謝!

+0

您可以發佈來自location.pathname和location.href的輸出示例以及您嘗試選擇的鏈接示例嗎?這對解決這個問題非常有幫助。 – alnorth29

+0

我添加了鏈接的源代碼以及我想要選擇的示例鏈接的location.pathname和location.href。 – Marta

回答

3

顯然location.href包含文本(協議和主機名:「http:// localhost /」)不在鏈接中。

在做比較之前,您需要從location.href中刪除它,或者將它添加到鏈接中。

+0

這就是答案。 – Mattis

+0

它似乎可能是解決方案。你知道如何從location.href中刪除域名(如將來的localhost或其他)嗎? – Marta

+0

類似'location.href.replace(/.*\/\/[^\/]*/,'')'可能會有效。這取代了第三次斜線。我不能保證正則表達式總能正常工作,因爲它是我剛纔快速放在一起的正則表達式。 – alnorth29

1

這只是location.hrefwindow.location.href而不是document.location.href

試試這個

function markActiveLink() { 

      var path = location.pathname; 
      var home = "/", $this, href, locationHref = location.href.toLowerCase(); 

      if (path == home) 
       return; 

      $("ul li").removeClass('menu_item'); 

      $("ul a").each(function() { 
       $this = $(this); 
       href = $this.attr("href").toLowerCase(); 
       if(locationHref.substring(locationHref.length - href.length) == href) 
       { 
        $this.closest("li").addClass("menu_item_active"); 
        return false; 
       } 
      }); 
     } 
+0

我已經嘗試使用location.href或window.location.href而不是location.pathname,並且它們都不工作 - >鏈接未被選中。 – Marta

+0

你可以發佈你的標記的一部分? – ShankarSangoli

+0

請檢查我編輯的答案我希望它會幫助你。 – ShankarSangoli

2

使用字符串連接到包括查詢:

var path = location.pathname + location.search; 
+0

我檢查了它,它工作得很好。但是,當主頁加載時,它從一開始就不起作用 - 菜單中的主頁鏈接未被選中。我必須點擊菜單項(主頁或其他),然後再次加載頁面,然後右菜單項被選中。 – Marta