jQuery click / toggle between two functions

前端 未结 10 1663
野的像风
野的像风 2020-11-22 07:58

I am looking for a way to have two separate operations / functions / \"blocks of code\" run when something is clicked and then a totally different block when the same thing

10条回答
  •  情深已故
    2020-11-22 08:44

    DEMO

    .one() documentation.

    I am very late to answer but i think it's shortest code and might help.

    function handler1() {
        alert('First handler: ' + $(this).text());
        $(this).one("click", handler2);
    }
    function handler2() {
        alert('Second handler: ' + $(this).text());
        $(this).one("click", handler1);
    }
    $("div").one("click", handler1);
    

    DEMO With Op's Code

    function handler1() {
        $(this).animate({
            width: "260px"
        }, 1500);
        $(this).one("click", handler2);
    }
    
    function handler2() {
        $(this).animate({
            width: "30px"
        }, 1500);
        $(this).one("click", handler1);
    }
    $("#time").one("click", handler1);
    

提交回复
热议问题