jquery - 使用 div 显示在两个选项卡/表格之间切换

标签 jquery css asp.net visual-studio-2012

我想在 aspx 页面中的两个选项卡(列表)之间切换。

有两个带有 div 标签的标签以 style="display: block;"开头和样式 =“显示:无;”分别。

我想在用户单击选项卡时在两者之间切换。这是我的代码。

<ul class="something" role="tablist">
    <li tabindex="0" class="random1" role="tab" aria-selected="true" aria-controls="application-tabs-1" aria-labelledby="ui-id-1"><a tabindex="-1" class="ui-tabs-anchor" id="ui-id-1" role="presentation" href="#application-tabs-1">Hello</a></li>
    <li tabindex="-1" class="random2" role="tab" aria-selected="false" aria-controls="application-tabs-2" aria-labelledby="ui-id-2"><a tabindex="-1" class="ui-tabs-anchor" id="ui-id-2" role="presentation" href="#application-tabs-2">World</a></li>
</ul>

<div class="something-else1" id="application-tabs-1" role="tabpanel" aria-expanded="true" aria-hidden="false" aria-labelledby="ui-id-1" style="display: block;">
    <table class="applications">
        <thead>
            <tr>
                <th>Java</th><th>C</th>
            </tr>
        </thead>
    </table>
</div>


<div class="something-else2" id="application-tabs-2" role="tabpanel" aria-expanded="false" aria-hidden="true" aria-labelledby="ui-id-2" style="display: none;">
    <table class="applications">
        <thead>
            <tr>
                <th>English</th><th>French</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

我知道我需要创建一个 jQuery 来使用 ID 在表之间切换,我尝试了不同的选项,但没有一个有效。

选项1

<script>
   $("#application-tabs-2").show();
   $("#application-tabs-2").hide();
</script>

选项 2

<script>
   $("#application-tabs-2").css("display", "block");
   $("#application-tabs-2").css("display", "none");
</script>

选项 3

<script>
   $(document).ready(function displayChange(){
     $("application-tabs-1").toggle();
     $("application-tabs-2").toggle();
   });
</script>

我该如何解决这个问题?

最佳答案

单击选项卡后,您将需要调用一个函数。在函数中,找到该选项卡的 href 以显示它,并隐藏所有其他 sibling (注意我排除了类为“something”的 ul):

$(".ui-tabs-anchor").click(function(event) {
    $("div#"+this.getAttribute('href')).show();
    $("div#"+this.getAttribute('href')).siblings().not('.something').hide();
});

关于jquery - 使用 div 显示在两个选项卡/表格之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31329787/

相关文章:

javascript - 旋转时使用拖动 handle 调整 div 大小

javascript - html() 操作中的 jquery 函数

jquery - 页面之间平滑滚动

css - 如何选择前面没有另一个特定元素的元素?

.net - 在 VS 2010 中调试时修改代码

asp.net - 部署后更改 asp.net Web 服务命名空间

javascript - GSAP:当进度条击中元素时固定进度条+进度条不准确

javascript - 输入到 div 大小

html - 强制水平滚动条

mysql - 无法使用 nuget 在 Visual Studio 中安装 MySql.Data