【问题标题】:add cssClass to CurrentNode in asp.net Navigation在 asp.net 导航中将 cssClass 添加到 CurrentNode
【发布时间】:2012-03-23 20:49:43
【问题描述】:

这是我的代码:

<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 那么它应该从 &lt;div class="right-bar-nav-item"&gt; 更改为 &lt;div class="right-bar-nav-item selected"&gt;

【问题讨论】:

    标签: c# asp.net navigation


    【解决方案1】:
    $(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 中执行此操作的方法。

    【讨论】:

    • cool ,使用 javascript 是我的计划 B。我将 '.menu a' 更改为 '.right-bar-nav-item' 并将 $(this).addClass('selected') 更改为 $( this).closest('div.right-bar-nav-item').addClass('selected') 现在它工作了
    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2016-04-04
    • 2011-09-05
    相关资源
    最近更新 更多