【问题标题】:add active class to current hyperlink using asp.net c#使用asp.net c#将活动类添加到当前超链接
【发布时间】:2014-08-12 17:37:53
【问题描述】:

我正在尝试使用 aspx 网络表单中的 j 查询来显示当前处于活动状态的链接。
首先,我通过这个函数从后面的代码生成了从我的数据库到 gallery.aspx 的所有超链接:

public string build_Gallery_Category()
{
    string post = "";
    DataTable dt = new DataTable();
    clsBusiness cls = new clsBusiness();
    dt = cls.Fill_In_DataTable("GalleryGroup");
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            post += "<li><a href='gallery.aspx?gid=" + dt.Rows[i]["GalleryId"].ToString() + "'><strong>" + dt.Rows[i]["GalleryName"] + "</strong></a></li>";
        }
    }
    return post;
}

所以,在我调用该方法后,所有超链接都会出现在 gallery.aspx 上,如下所示:

<div id="menu">
    <ul>
       <%=viewCategory %>
    </ul>
</div>

之后,将此样式添加到 gallery.aspx:

<style>
    .active { background: #f00; }
</style>

毕竟,我在 from 的末尾添加了 jquery 代码:

<script>
    $("menu li").click(function (e) {
        e.preventDefault();
        $("menu li a.active").removeClass("active"); //Remove any "active" class  
        $("a", this).addClass("active"); //Add "active" class to selected tab  
    });
</script>

但我的代码不起作用......所以请帮助我。非常感谢...

【问题讨论】:

  • build_Gallery_Categorygallery.aspx?
  • 不,它在单独的类中并从 gallery.aspx.cs 调用它

标签: c# jquery css asp.net


【解决方案1】:

看起来你的 jQuery 选择器是错误的

$("menu li")

必须

$("#menu li")

$("menu li a.active")

必须

$("#menu li a.active")

没有 # 您无法访问带有 id 菜单的 div 标签

【讨论】:

  • 谢谢你,但现在,超链接 href 已禁用并且不会向服务器发送查询。
  • 我认为这是因为 e.preventDefault();如果您单击链接,也会单击 li 标记并阻止进一步的操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2019-06-24
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多