2

I have a problem where I have to click twice in order for my JavaScript function to run.

HTML for the button:

<input type="button" id="logintrigger" value="Log In" onClick="hideshow();"/>

JavaScript:

function hideshow() {
    var toggle = document.getElementById('logincontainer');
    toggle.style.display = toggle.style.display == "none" ? "block" : "none";
}

I have been searching around and haven't found anything that helped.

Thank You.

4

2 回答 2

5

style="display:block"显式应用于 logincontainer,然后更改以下行: toggle.style.display = toggle.style.display == "block" ?“无”:“块”

于 2012-07-29T19:59:26.530 回答
2

The initial state of display property is more likely to be empty. That's why till you explicitly change it - this js function gets "".

于 2012-07-29T19:53:01.773 回答