admin管理员组

文章数量:1313121

By using this method I can show/hide an element by using 2 buttons:

<script type="text/javascript">
function showStuff(id) {
        document.getElementById(id).style.display = 'block';
}
function hideStuff(id) {
    document.getElementById(id).style.display = 'none';
}
</script>

<input type="button" onClick="hideStuff('themes')" value="Hide">
<input type="button" onClick="showStuff('themes')" value="Show">
<div id="themes" style="display:block">
    <h3>Stuff</h3>
</div>

Is there a method to use a single button?? Maybe if & else?

By using this method I can show/hide an element by using 2 buttons:

<script type="text/javascript">
function showStuff(id) {
        document.getElementById(id).style.display = 'block';
}
function hideStuff(id) {
    document.getElementById(id).style.display = 'none';
}
</script>

<input type="button" onClick="hideStuff('themes')" value="Hide">
<input type="button" onClick="showStuff('themes')" value="Show">
<div id="themes" style="display:block">
    <h3>Stuff</h3>
</div>

Is there a method to use a single button?? Maybe if & else?

Share Improve this question asked Dec 17, 2012 at 19:44 Onyx G.Onyx G. 711 gold badge3 silver badges8 bronze badges 3
  • Use jQuery's toggle and have fun api.jquery./toggle – Andrey Kryachkov Commented Dec 17, 2012 at 19:47
  • Check .style.display's value first, then set its value accordingly. If it's hidden, show it. If it's shown, hide it. – Ian Commented Dec 17, 2012 at 19:47
  • 2 ca'nt be that hard, something like : style.display = style.display == 'none' ? 'block' : 'none'; – adeneo Commented Dec 17, 2012 at 19:52
Add a ment  | 

4 Answers 4

Reset to default 4

You've already answered your question...the use of if/else:

function toggle(id) {
    var element = document.getElementById(id);

    if (element) {
        var display = element.style.display;

        if (display == "none") {
            element.style.display = "block";
        } else {
            element.style.display = "none";
        }
    }
}

This won't be pletely foolproof, in case you are hiding/showing inline or inline-block elements, or if you are using non-default values for display on elements...such as setting a div's display to "inline" (for whatever reason) and then trying to use this function

Yes if/else will work

function toggle(id){
    var elem = document.getElementById(id);
    if(elem.style.display == 'block'){
        elem.style.display == 'none'
    } else if(elem.style.display == 'none'){
        elem.style.display == 'block'
    }
}

I think you mean something like this:

 function toggle(id){
var elem = document.getElementById(id);
if(elem.style.display == "block"){
    elem.style.display="none";
} else {
    elem.style.display="block";
}

}

Here is an alternate solution. Instead of using document.getElementById, you can also use document.querySelector which returns the first Element within the document that matches the specified selector, or group of selectors.

Solution Code:

function toggleShow() {
    var elem = document.querySelector(id);
    if(elem.style.display == 'inline-block'){
      elem.style.display="none";
    }
    else {
      elem.style.display = "inline-block";
    }
  }

本文标签: htmlToggle (showhide) element with javascriptStack Overflow