使用 2 个不同的切换功能切换同一表行

发布于 2024-10-13 17:49:20 字数 2133 浏览 8 评论 0原文

我试图通过两个不同的来源切换详细信息行。

  1. 如果用户单击“名称”或“地址警报”,则显示或隐藏该特定详细信息行。
  2. 如果用户单击“切换全部”,则显示或隐藏所有详细信息行。

问题是两个独立的切换函数不知道另一个函数在做什么。因此,例如,如果刚刚单击“切换全部”,现在显示所有详细信息行,则单击单个名称应隐藏该详细信息行。但是,由于单个切换功能最多可“显示”,因此需要单击 2 次才能隐藏该行的详细信息。

HTML:

<div id="toggleAll"></div>
<table>
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
    <tr class="details"></tr>
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
    <tr class="details"></tr>
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
    <tr class="details"></tr>
</table>

JavaScript:

//toggles beween showing and hiding the details for specific row
$(
    function(){
        //if click on carrier name or address alert symbol
        $('.name, .addressAlert').toggle(
            function() {
            //gets the row clicked on, and finds the next row (the details row)
            detailsTds = $(this).parents("tr.infoRow").next();
            //adds the "shown" class, which sets the display to table-row
            detailsTds.addClass("shown");
            },
            function(){
            //gets the row clicked on, and finds the next row (the details row)
            detailsTds = $(this).parents("tr.infoRow").next();
            //removes the "shown" class, thereby setting the display to none
            detailsTds.removeClass("shown");
            }
        );  
    }
);

//toggle ALL details 
$(
    function(){
        //if click on carrier name or address alert symbol
        $('#toggleAll').toggle(
            function() {
            $('.details').addClass("shown");
            $('#toggleAll').text('[-] Hide all addresses');
            },
            function(){
            $('.details').removeClass("shown");
            $('#toggleAll').text('[+] Show all addresses');
            }
        );  
    }
);

I am attempting to toggle a details row via two different sources.

  1. If the user clicks on either the Name or the AddressAlert, then that specific detail row gets shown or hidden
  2. If the user clicks on "toggle all" then ALL the details rows get shown or hidden.

The issue is that the two separate toggle functions don't know what the other one is up to. So, for example, if the "toggle all" was just clicked, and now all the details rows are shown, clicking on an individual Name should hide that details row. However, since the individual toggle function is up to "show", it takes 2 clicks to hide the details for that row.

the HTML:

<div id="toggleAll"></div>
<table>
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
    <tr class="details"></tr>
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
    <tr class="details"></tr>
    <tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
    <tr class="details"></tr>
</table>

The javascript:

//toggles beween showing and hiding the details for specific row
$(
    function(){
        //if click on carrier name or address alert symbol
        $('.name, .addressAlert').toggle(
            function() {
            //gets the row clicked on, and finds the next row (the details row)
            detailsTds = $(this).parents("tr.infoRow").next();
            //adds the "shown" class, which sets the display to table-row
            detailsTds.addClass("shown");
            },
            function(){
            //gets the row clicked on, and finds the next row (the details row)
            detailsTds = $(this).parents("tr.infoRow").next();
            //removes the "shown" class, thereby setting the display to none
            detailsTds.removeClass("shown");
            }
        );  
    }
);

//toggle ALL details 
$(
    function(){
        //if click on carrier name or address alert symbol
        $('#toggleAll').toggle(
            function() {
            $('.details').addClass("shown");
            $('#toggleAll').text('[-] Hide all addresses');
            },
            function(){
            $('.details').removeClass("shown");
            $('#toggleAll').text('[+] Show all addresses');
            }
        );  
    }
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只怪假的太真实 2024-10-20 17:49:20

您可以使用click()而不是toggle(),然后根据当前应用于该行的类来显示或隐藏。

You could use click() instead of toggle(), then show or hide based on the class that is currently applied to the row.

别想她 2024-10-20 17:49:20

可以直接引用类吗?

Could you reference directly to the class?

安静 2024-10-20 17:49:20

为什么不替换第一个切换中的功能:
“$('.name, .addressAlert').toggle”

,然后使用显示的类初始化所有 infoRows-
因此,即使用户按下“切换全部”,第一次按下它也会显示

why don't you replace the functions in the first toggle:
"$('.name, .addressAlert').toggle"

and then initialize all the infoRows with shown class-
so the first time it will be pressed it will be shown, even if the user pressed the "toggle ALL"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文