简体   繁体   English

jQuery切换功能打开/关闭

[英]jquery toggle function open/close

Trying to do a function that does "A" when toggle open and "B" when toggle close. 尝试执行一个功能,当扳机打开时执行“ A”,当扳机关闭时执行“ B”。

Code: 码:

function awesome_toggle()
{
    $(".button").click(function()
    {
        $("content").toggle(
            function()
            {
                /* Do "A" */
            },
            function()
            {
                /* Do "B" */
            }
        );
    });
}

This code doesn't seem to work and I can't figure out why. 这段代码似乎无效,我也不知道为什么。 When I use only one function, so I stop the code after /* Do "A" */ it works but when I add the /* Do "B" */ function it stops working. 当我仅使用一个函数时,因此我在/ * Do“ A” * /之后停止了代码,但是在我添加/ * Do“ B” * /函数之后,它停止了工作。 What is wrong? 怎么了?

jquery version: ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js jQuery版本:ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js

As the comments have said, this version of toggle has been deprecated. 正如评论所说,此版本的toggle已被弃用。

There may very well be a better way to accomplish what you want, but you can check whether the element is visible after toggle with the :visible selector, and do stuff conditionally based upon that. 可能有更好的方法来完成所需的操作,但是您可以使用:visible选择器切换后检查元素是否可见,并根据该条件有条件地进行处理。

function awesome_toggle()
{
    $(".button").click(function() {
        $("content").toggle(normalSpeed, function () {
            if ($("content").is(':visible')) {
                /* Do "A" */
            } else {
                /* Do "B" */
            }
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM