最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery toggle function openclose - Stack Overflow

programmeradmin1浏览0评论

Trying to do a function that does "A" when toggle open and "B" when toggle close.

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. What is wrong?

jquery version: ajax.googleapis/ajax/libs/jquery/1.8.3/jquery.min.js

Trying to do a function that does "A" when toggle open and "B" when toggle close.

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. What is wrong?

jquery version: ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js

Share Improve this question edited Mar 25, 2013 at 18:50 Notsogood13 asked Mar 25, 2013 at 18:44 Notsogood13Notsogood13 1091 gold badge4 silver badges11 bronze badges 8
  • is this your plete JS? – Mooseman Commented Mar 25, 2013 at 18:45
  • @Mooseman If you are implying this should be re-tagged as a JavaScript question (adding the JS tag) I think you should do that. Also, OP, I'm not sure I understand the question, specifically what you'd like done. – Benjamin Gruenbaum Commented Mar 25, 2013 at 18:46
  • This two-function version of toggle was removed in jQuery 1.9.1. Have a look at the documentation: api.jquery./toggle. I believe it is now interpreting the first function as something else (e.g. a duration) and throw an error. – Felix Kling Commented Mar 25, 2013 at 18:46
  • @user2084627: That version of toggle was deprecated, and it was removed in jQuery 1.9.1. Perhaps that's the problem? Also, "pretty much" really isn't good enough. – T.J. Crowder Commented Mar 25, 2013 at 18:47
  • What version of jQuery are you including? – Mooseman Commented Mar 25, 2013 at 18:47
 |  Show 3 more ments

1 Answer 1

Reset to default 6

As the ments have said, this version of toggle has been deprecated.

There may very well be a better way to acplish 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.

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

评论列表(0)

  1. 暂无评论