如何检查 JQuery 滑动切换的状态?

发布于 2025-01-05 10:20:34 字数 796 浏览 3 评论 0原文

因此,我有一个手风琴菜单来显示结果,并在顶部有一个“显示全部”按钮,这样用户就不必单击每个菜单来展开它。当他们单击“显示全部”时,所有内容都会展开,并且按钮的文本会切换为“隐藏全部”。

但是,我想确保,如果用户决定自己手动关闭所有部分,则按钮将再次从“隐藏全部”切换到“显示全部”。

我也希望这能以另一种方式工作,以防万一他们打开所有这些(或者可能是任意数量的)。如果我能按下按钮说“隐藏全部”,那就太好了。

有道理吗?它应该相对简单......我只是 JQuery 的新手,我不知道如何检查切换的状态。这是我必须展开所有部分的代码。

    function expandAll() {
        if(document.getElementById("displayMajors").innerHTML == "Show All") {
            $('.accordionContent').slideDown('normal');
            document.getElementById("displayMajors").innerHTML = "Hide All";
        }
        else { 
            $('.accordionContent').slideUp('normal');
            document.getElementById("displayMajors").innerHTML = "Show All";
        }
     }

“displayMajors”是按钮的 ID,“accordionContent”是 div 类。

So I have an accordion menu to display results and a "Show All" button at the top so that the user doesn't have to click each one to expand it. When they click "Show All", all of the content expands out, and the button's text switches to say "Hide All".

However, I want to make sure that if the user decides to manually close all of the sections himself, then the button will switch from "Hide All" to "Show All" again.

I'd like this to work the other way too, just in case they open all of them (or perhaps any number of them). It'd be nice if I could get the button to then say "Hide All".

Make sense? It should be relatively simple...I'm just new to JQuery, and I'm not sure how to check the state of the toggle. Here's the code I have to expand all of the sections.

    function expandAll() {
        if(document.getElementById("displayMajors").innerHTML == "Show All") {
            $('.accordionContent').slideDown('normal');
            document.getElementById("displayMajors").innerHTML = "Hide All";
        }
        else { 
            $('.accordionContent').slideUp('normal');
            document.getElementById("displayMajors").innerHTML = "Show All";
        }
     }

"displayMajors" is the ID of the button, and "accordionContent" is a div class.

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

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

发布评论

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

评论(1

马蹄踏│碎落叶 2025-01-12 10:20:35

添加测试函数

function test_accordion() {
    var elems = $('.accordionContent');
    if (elems.filter(':visible').length == 0) // all hidden
        $("#displayMajors").html("Show All");
    else if (elems.filter(':hidden').length == 0) // all visible
        $("#displayMajors").html("Hide All");
}

每当向上(或向下)滑动元素时,请添加测试函数作为回调函数。它将在动画完全完成时执行:

$(some_element).slideUp(test_accordion);
$(some_element).slideDown(test_accordion);

Add a test function

function test_accordion() {
    var elems = $('.accordionContent');
    if (elems.filter(':visible').length == 0) // all hidden
        $("#displayMajors").html("Show All");
    else if (elems.filter(':hidden').length == 0) // all visible
        $("#displayMajors").html("Hide All");
}

Whenever you slide an element up (or down), add the test function as the callback function. It will be executed when the animation's finished completely:

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