在asp.net Navigation中将cssClass添加到CurrentNode

时间:2012-03-23 20:49:44

标签: c# asp.net navigation

这是我的代码:

<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">

1 个答案:

答案 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中执行此操作。