ASPX Bootstrap标签类的访问问题

问题描述 投票:0回答:1

我正在使用bootstrap nav-tabs,我遇到了一个问题:当[li]标签被选中时,我无法从代码后台获取其属性 "class "的值,它总是返回null。它总是返回null.这是我的aspx。

<!-- This is the js to prevent losing active tab after postback -->
<script type="text/javascript">
    $(function () {
        var tabName = $("[id*=TabName]").val() != "" ? $("[id*=TabName]").val() : "personal";
        $('#Tabs a[href="#' + tabName + '"]').tab('show');
        $("#Tabs a").click(function () {
            $("[id*=TabName]").val($(this).attr("href").replace("#", ""));
        });
    });
</script>
<div id="Tabs" role="tabpanel">
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li runat="server" id="Tab1" role="tab">
            <a href="#personal" aria-controls="personal" role="tab" data-toggle="tab">Personal</a>
        </li>
        <li runat="server" id="Tab2" role="tab">
            <a href="#employment" aria-controls="employment" role="tab" data-toggle="tab">Employment</a>
        </li>
    </ul>
    <!-- Tab panes -->
    <div class="tab-content" style="padding-top: 20px">
        <div role="tabpanel" class="tab-pane active" id="personal">
            This is Personal Information Tab
        </div>
        <div role="tabpanel" class="tab-pane" id="employment">
            This is Employment Information Tab
        </div>
    </div>
</div>

<asp:Button ID="Button1" Text="Submit" runat="server" CssClass="btn btn-primary" OnClick="Button1_Click" />
<asp:HiddenField ID="TabName" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  • 代码后台。

protected void Button1_Click(object sender, EventArgs e)
{
    string cl1 = Tab1.Attributes["class"];
    string ro1 = Tab1.Attributes["role"];
    Label1.Text = "class of tab1: " + cl1 + "<br />role: " + ro1;
}
- 当我在Tab1被选中时点击button1 --> 我的结果显示在Label1上。
class of tab1: {nothing}
role: tab

From the same tag [li], attribute 'role' can be retrieved, but 'class' is null.I have no idea about this indeed, because when I inspect the element, the class of that [li] is 'active'I tried googling around but unable to find similar questions.Please help me with this, thanks!!!

javascript html asp.net attributes postback
1个回答
-1
投票

<li runat="server" class="yourclass" id="yourid" >

把runat="server "属性加到你的li元素上 然后从你的代码后面调用li的id 比如说你的id.Attributes 你可以为元素添加或删除类

© www.soinside.com 2019 - 2024. All rights reserved.