How to make a boolean variable switch between true and false every time a method is invoked?

后端 未结 9 1004
南旧
南旧 2020-12-13 09:01

I am trying to write a method that when invoked, changes a boolean variable to true, and when invoked again, changes the same variable to false, etc.

For example: ca

9条回答
  •  醉梦人生
    2020-12-13 09:25

    var logged_in = false;
    logged_in = !logged_in;
    

    A little example:

    var logged_in = false;
    
    
    $("#enable").click(function() {
        logged_in = !logged_in;
        checkLogin();
    });
    
    function checkLogin(){
        if (logged_in)
            $("#id_test").removeClass("test").addClass("test_hidde");
        else
            $("#id_test").removeClass("test_hidde").addClass("test");
        $("#id_test").text($("#id_test").text()+', '+logged_in);
    }
    .test{
        color: red;
        font-size: 16px;
        width: 100000px
    }
    
    .test_hidde{
        color: #000;
        font-size: 26px;
    }
    
    
    Some Content...

提交回复
热议问题