2017-03-19 94 views
1

嘗試在單擊按鈕時在不同div之間切換。無法在html中的不同div部分之間切換

<button onclick="myFunction('myDIV')">Try it</button> 
<button onclick="myFunction('myDIV2')">Try it 2</button> 
<button onclick="myFunction('myDIV3')">Try it 3</button> 

這些事業部,我想執行

<div id="myDIV" style="display:none;"> 
This is my DIV element. 
</div> 

<div id="myDIV2" style="display:none;"> 
This is second DIV element. 
</div> 

<div id="myDIV3" style="display:none;"> 
This is 3rd DIV element. 
</div> 

的JavaScript代碼來處理按鈕點擊後點擊它會永遠指向DIV3股利

function myFunction(id) { 
var divs = ["myDIV","myDIV2","myDIV3","myDIV4","myDIV5"]; 
for(i = 0; i < 5; i++){ 
if(divs[i] == id) 
    {document.getElementById(id).style.display = "block";} 
else 
    {document.getElementById(id).style.display = "none";} 
document.getElementById("debug").innerHTML = divs[i]; 
} 

之間切換,請幫幫我。謝謝。

+0

你怎麼罵你的函數? – CodeIsLife

+0

,點擊這個按鈕 – Sivabushan

回答

2

你應該使用的div [I],而不是帕拉姆

function myFunction(id) { 
    var divs = ["myDIV","myDIV2","myDIV3","myDIV4","myDIV5"]; 
    for(i = 0; i < 5; i++){ 
    if(divs[i] == id) 
     {document.getElementById(divs[i]).style.display = "block";} 
    else 
     {document.getElementById(divs[i]).style.display = "none";} 
    document.getElementById("debug").innerHTML = divs[i]; 
    } 
+0

感覺很尷尬,找到我愚蠢的錯誤。 – Sivabushan

+0

不用擔心 – CodeIsLife