使用按钮显示/隐藏div的按钮(并在按钮之间切换)

时间:2014-02-17 15:41:23

标签: javascript html button hide show

我正在寻找一个按钮功能,用javascript显示/隐藏div,并让按钮在彼此之间切换。

我可以让它们显示和隐藏,但是如果没有显示其他div,则无法让按钮在彼此之间切换。

<script type="text/javascript" language="javascript">
function showHide() {
var ele = document.getElementById("showHideDiv");
if(ele.style.display == "block") {
        ele.style.display = "none";
  }
else {
    ele.style.display = "block";
}
}

function showHide1() {
var ele = document.getElementById("showHideDiv1");
if(ele.style.display == "block") {
        ele.style.display = "none";
  }
else {
    ele.style.display = "block";
}
}
</script>

<button onclick="return showHide();">box1</button>
<button onclick="return showHide1();">box2</button>
<div id="showHideDiv" style="display:none;">text1</div>
<div id="showHideDiv1" style="display:none;">text2</div>

1 个答案:

答案 0 :(得分:1)

如果我的问题正确,可能会有用

<script type="text/javascript" language="javascript">
    function showHide() {
        var ele = document.getElementById("showHideDiv");
        var ele1 = document.getElementById("showHideDiv1");
        ele1.style.display = "none";
        if(ele.style.display == "block") {
                ele.style.display = "none";             
          }
        else {
            ele.style.display = "block";            
        }
    }

    function showHide1() {
        var ele = document.getElementById("showHideDiv");
        var ele1 = document.getElementById("showHideDiv1");
        ele.style.display = "none";
        if(ele1.style.display == "block") {
                ele1.style.display = "none";
          }
        else {
            ele1.style.display = "block";
        }
    }
</script>

<button onclick="return showHide();">box1</button>
<button onclick="return showHide1();">box2</button>
<div id="showHideDiv" style="display:none;">text1</div>
<div id="showHideDiv1" style="display:none;">text2</div>