简体   繁体   中英

Javascript/Jquery Boolean help: Hiding/Showing Divs

Can someone explain to me what i am doing wrong in this code?

http://jsfiddle.net/14njfqef/

var isLoggedIn = function(state){
        if(state == true) {
            $("#content-container").show();
            $("#account2").show();
            $("#account").hide();
        }
        else(state == false){
            $("#content-container").hide();
            $("#account2").hide();
            $("#account").show();
        }
    }

onload=function() {
            isLoggedIn(false);
    }

On load i want the divs to hide but then when i click the button i want the divs to show?

Is the boolean function set out in the correct way?

Piece below tries to re-arrange piece at OP. onload not appear clearly defined , not addressed , though could be attached to an event , ieg, window.onload = onload . Wrapped blocks in jquery .ready() event . Removed js onclick markup from html , included at script element , or loaded from file at jquery .on("click") event . Added strict comparison operator === (an added = ) to if / else if statements. Changed input type to button. Added if to else portion of composition (see link posted at comments by Felix Kling).

Try

$(function() {
var isLoggedIn = function(state){
        if(state === true) {
            $("#content-container").show();
            $("#account2").show();
            $("#account").hide();
        }
        else if(state === false){
            $("#content-container").hide();
            $("#account2").hide();
            $("#account").show();
        }
    };

    isLoggedIn(false);

    $("input[type=button]").click(function() {
      isLoggedIn(true)
    })
});

jsfiddle http://jsfiddle.net/guest271314/14njfqef/3/

I am assuming you have JQuery library loaded. Try

if (state) {
        $("#content-container").show();
        $("#account2").show();
        $("#account").hide();

}
else{

     $("#content-container").hide();
        $("#account2").hide();
        $("#account").show();
}

to solve your problem.

changed your html to

<input type="submit" value="Boolean" id="toggle"/>

rewrote your js as

// JQuery run at start effectivly
$(document).ready(function() {

    function isLoggedIn(state) {
        if(state == true) {

            $("#content-container").show();
            $("#account2").show();
            $("#account").hide();
        }
        else {

            $("#content-container").hide();
            $("#account2").hide();
            $("#account").show();
        }
    }

    // JQuery attaching a click event using an anonymous function 
    // and hard coding your isLoggedIn to true, passing variables is a bit more complicated.
    $('#toggle').click(function() {isLoggedIn(true)});

    isLoggedIn(false);
})

Well there's a few things I am not sure if you are aware of so I feel there's some responsibility on my end to make sure they are mentioned. They are a number of syntactical errors in your post that are stopping this from working so instead of addressing them I feel its necessary to update your view on what JQuery you are using as well as your selector choice.

First I would add a class structure to all of the div's to target them all at once so you can save on some lines of code. In production it's always better to have less code for all of your visitors to download because even a little bit of code can get out of control after enough hits on a webpage. Having to serve it kills speed and so does having to process three separate jquery selections as opposed to one.

I would change the HTML to...

<body>
    <div id='content-container' class='boxes'>
        Content Container
    </div>
    <div id='account' class='boxes'>
        account
    </div>
    <div id='account2' class='boxes'>
        account2
    </div>
    <input id="validateButton" type="submit" value="Boolean">
</body>

This way you can simply target all divs with $(".boxes"); ... I wouldn't recommend getting into the habbit of using $("div");

Next I would change the JQuery to being more JQuery friendly code. Its not always useful to use an onload event from pure Javascript to handle JQuery driven functions in correct time to the loading of DOM objects. Therefore you should use $( document ).ready( handler ) to handle this load event properly just in case it causes you problems down the road. The more common shorthand of this ready event is a simple $(function() { }); wrapper.

The rest of the code can be re-arranged to this....

var isLoggedIn = false; //<--Instantiate to false, make global to window level scope

//Load event Corrected For JQuery
$(function() {
    $(".boxes").hide(); //<--Hide on load

    //Add A Proper Updated Click Event To Button
    $("#validateButton").click(function() {
        isLoggedIn = true; //<--Should include real functionality not hand coded to true
        checkLoginAndRespond(); //<--Validate Login Status
    });
});

function checkLoginAndRespond() {
    //If Logged, Show
    if(isLoggedIn) {
        $(".boxes").show();

    //Else Don't
    } else { $(".boxes").hide(); }

} //end function

Lastly, the version. New versions of JQuery have not been released for some time and seem to not be in the making so its a safe bet to use their most recent versions as it has thousands of pages of help for its syntax and it's very stable. I would recommend anything in the 2.0 or higher series JQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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