如何使用位置.Href代替location.pathname
How to use location.href instead of location.pathname
我的Javascript函数是这样的:
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>
和这些链接的位置值。pathname将只有/App/User/UserOrder.aspx
,我需要检查整个链接。这就是为什么我尝试使用位置。href。
位置。例如:http://localhost/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO
然后是位置。路径名是:/App/User/UserOrder.aspx
任何帮助在这里非常感激!
使用字符串连接来包含查询:
var path = location.pathname + location.search;
明显位置。Href包含不在链接中的文本(协议和主机名:"http://localhost/")。
你要么需要从位置中删除这个。
只有location.href
或window.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;
}
});
}
相关文章:
- 正在使用$location.path(.)路由ng视图
- Reg-ex,用于解析window.location.pathname并返回个/s
- addClass('active') with window.location.pathname - u
- Javascript - 获取 location.pathname + location.search - 不占用所有部
- window.location.pathname Chrome vs Mozilla
- parent.window.location.pathname 返回未定义
- windows.location.pathname不起作用
- 使用history.pushState()时更新整个location.pathName
- 将location.pathname和location.search一起设置
- 如何“;转换“;window.location.pathname从对象到字符串
- 哪个更快:document.location.pathname或myOwnObj.pathname
- 在location.pathname前添加哈希值
- what return 'windows.location.pathname'?
- 如何使用位置.Href代替location.pathname
- 如何检查数组中的字符串是否在URL/window.location.pathname中
- 如何从window.location.pathname中删除尾部斜杠
- 如何防止window.location.pathname在URL中转义字符
- 如何从window.location.pathname获取文件名
- 相当于NodeJS中的window.location和window.pathname
- # for window.location.pathname