2014-02-17 404 views
0

我正在尋找使按鈕顯示/隱藏div的按鈕功能,並讓按鈕相互之間切換。在一個按鈕上按下按鈕顯示/隱藏div的javascript按鈕(並在按鈕之間切換)

我可以讓他們顯示和隱藏,但不能讓按鈕之間切換而不顯示其他股利。

<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> 
+0

你的「互相之間切換」的意思的。當第一個div可見時,你想隱藏第二個div嗎? – Ovidiu

+0

是的,是的,我做 – Obon

回答

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> 
+0

不是它在div之間切換,但它dosnt再次隱藏div – Obon

+0

@Obon我編輯了我的答案 – Ovidiu

+0

作品魅力!謝謝 – Obon