这是我的代码:
<div class="Cat-list unselectable" style="font-weight:bold;">
<div class="right-bar-nav-item">
<asp:HyperLink runat="server" ID="lnkHome" NavigateUrl="~/Default.aspx">Home</asp:HyperLink>
</div>
<asp:Repeater runat="server" ID="menu" DataSourceID="SiteMapDataSource1">
<ItemTemplate>
<div class="right-bar-nav-item">
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' ToolTip='<%# Eval("description") %>'><%# Eval("Title") %></asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div style="clear:both;"></div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
我想将“.selected”类赋予当前节点。示例:如果其Default.aspx则应从<div class="right-bar-nav-item">
更改为此<div class="right-bar-nav-item selected">
答案 0 :(得分:2)
$(function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
// now grab every link from the navigation
$('.menu a').each(function(){
// and test its normalized href against the url pathname regexp
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('selected');
}
});
});
以下是如何在javascript中执行此操作。