jQuery - 切换按钮显示/隐藏表数据

时间:2016-02-03 10:32:15

标签: javascript jquery html

我想请一些帮助。

<h2>Operating systems <span class="button small toggle-btn">Toggle</span></h2>
<table class="data" cellpadding="8" border="1">
    <thead>
        <tr>
            <th class="first" style="width:120px">
                Operating System </th>
            <th class="">
                Percentage </th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <td class="">iOS 8.4</td>
            <td class="">
                <div class="ui-percentage" style="width:17%">17 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.3</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.2</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.1.3</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.1</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>
    </tbody>
</table>

第一次加载页面时,我想要折叠表格。当我单击切换按钮时,我想更改表状态,所以基本上要扩展它并再次折叠它再次点击按钮。

这是我的剧本

<script>
    $(document).ready(function() {
        $('.toggle-btn').closest('table').hide(); // I want to target the table right after the button
        $('.toggle-btn').on('click', function(event) {
            $(this).closest('table').toggle();
        });
    });
</script>

如何才能使这项工作正确?

4 个答案:

答案 0 :(得分:3)

为什么不直接使用切换功能?

$(document).ready(function() {
    $('table').hide();
    $('.toggle-btn').on('click', function() {
        $('table').toggle();
    });
});

答案 1 :(得分:2)

如果您只有一个 table,那么只执行

$(document).ready(function() {
        $('tbody').closest('h2').next().find("tbody").hide();
        $('.toggle-btn').on('click', function() {
            $('tbody').toggle();
        });
    });

对于多个兄弟表

您可以找到距离最近的h2next()可以找到table

&#13;
&#13;
$(document).ready(function() {
        $('.toggle-btn').closest('h2').next().find("tbody").hide();
        $('.toggle-btn').on('click', function() {
            $(this).closest('h2').next().find("tbody").toggle();
        });
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Operating systems <span class="button small toggle-btn">Toggle</span></h2>
<table class="data" cellpadding="8" border="1">
    <thead>
        <tr>
            <th class="first" style="width:120px">
                Operating System </th>
            <th class="">
                Percentage </th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <td class="">iOS 8.4</td>
            <td class="">
                <div class="ui-percentage" style="width:17%">17 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.3</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.2</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr class="tr even">
            <td class="">iOS 8.1.3</td>
            <td class="">
                <div class="ui-percentage" style="width:11%">11 %</div>
            </td>
        </tr>

        <tr>
            <td class="">iOS 8.1</td>
            <td class="">
                <div class="ui-percentage" style="width:6%">6 %</div>
            </td>
        </tr>
    </tbody>
</table>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

$(document).ready(function() {
  $('.toggle-btn').parent().next('table').hide(); // I want to target the table right after the button
  $('.toggle-btn').on('click', function(event) {
    $(this).parent().siblings('table').toggle();
  });
});

选择器应为$('.toggle-btn').parent().next('table')

DEMO

答案 3 :(得分:0)

函数ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 上升到元素的父元素,因此它永远不会找到表格。你必须做这样的事情:

closest()

但如果有多个表,这将无效。考虑使用id或表,以便您可以直接访问它。例如:

$(document).ready(function() {
    $('.toggle-btn').parent().parent().find('table').hide(); // I want to target the table right after the button
    $('.toggle-btn').on('click', function(event) {
        $(this).parent().parent().find('table').toggle();
    });
});

这会使javascript变得更简单,即:

<table id="data-table-1" class="data" cellpadding="8" border="1">
相关问题