我的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
任何這裏幫助非常感謝!
您可以發佈來自location.pathname和location.href的輸出示例以及您嘗試選擇的鏈接示例嗎?這對解決這個問題非常有幫助。 – alnorth29
我添加了鏈接的源代碼以及我想要選擇的示例鏈接的location.pathname和location.href。 – Marta