2017-02-03 57 views
1

我確定這是一個非常簡單的問題,但我無法用javascript打開兩個模式窗口。使用兩種模式

預期效果將是每個按鈕打開自己的模式

JS FIDDLE

HTML

<!-- Modal 1 --> 
<div id="myModal-1" class="modal"> 
    <div class="modal-content"> 
    <div class="box-content"> 
     <h2>Modal 1</h2> 
     <span class="close">&times;</span> 
    </div> 
    </div> 
</div> 

<!-- Modal 2 --> 
<div id="myModal-2" class="modal"> 
    <div class="modal-content"> 
    <div class="box-content"> 
     <h2>Modal 2</h2> 
     <span class="close">&times;</span> 
    </div> 
    </div> 
</div> 

<button id="modalBtn-1">Modal 1</button> 
<button id="modalBtn-2">Modal 2</button> 

JS

// Get the modal 1 
var modal = document.getElementById('myModal-1'); 

var btn = document.getElementById("modalBtn-1"); 

var span = document.getElementsByClassName("close")[0]; 

btn.onclick = function() { 
    modal.style.display = "block"; 
} 

span.onclick = function() { 
    modal.style.display = "none"; 
} 

window.onclick = function(event) { 
    if (event.target == modal) { 
     modal.style.display = "none"; 
    } 
} 
+1

爲什麼重新發明輪子的時候可以使用自舉模式? – jjj

回答

0

您遇到的問題是第二個功能覆蓋第一個的事件。

如果你有做多2個情態動詞,你可能要考慮修改的功能更加普遍

// Get the modal 
 
var modal = document.getElementById('myModal-1'); 
 

 
// Get the button that opens the modal 
 
var btn = document.getElementById("modalBtn-1"); 
 

 
// Get the <span> element that closes the modal 
 
var span = document.getElementsByClassName("close")[0]; 
 

 
// When the user clicks on the button, open the modal 
 
btn.onclick = function() { 
 
    modal.style.display = "block"; 
 
} 
 

 
// When the user clicks on <span> (x), close the modal 
 
span.onclick = function() { 
 
    modal.style.display = "none"; 
 
} 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
} 
 

 

 

 
// Get the modal 
 
var modal = document.getElementById('myModal-2'); 
 

 
// Get the button that opens the modal 
 
var btn = document.getElementById("modalBtn-2"); 
 

 
// Get the <span> element that closes the modal 
 
var span = document.getElementsByClassName("close")[0]; 
 

 
// When the user clicks on the button, open the modal 
 
btn.onclick = function() { 
 
    modal.style.display = "block"; 
 
} 
 

 
// When the user clicks on <span> (x), close the modal 
 
span.onclick = function() { 
 
    modal.style.display = "none"; 
 
} 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
}
.modal { 
 
    display: none; /* Hidden by default */ 
 
    position: fixed; /* Stay in place */ 
 
    z-index: 1; /* Sit on top */ 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; /* Full width */ 
 
    height: 100%; /* Full height */ 
 
    overflow: auto; /* Enable scroll if needed */ 
 
    background-color: rgb(0,0,0); /* Fallback color */ 
 
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
 
} 
 

 
/* Modal Content/Box */ 
 
.modal-content { 
 
    margin: 10% auto; /* 15% from the top and centered */ 
 
    max-width: 800px; /* Could be more or less, depending on screen size */ 
 
} 
 

 
/* The Close Button */ 
 
.close { 
 
    color: #aaa; 
 
    float: right; 
 
    font-size: 28px; 
 
    font-weight: bold; 
 
} 
 

 
.close:hover, .close:focus { 
 
    color: black; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
} 
 
.box-content { 
 
    background: #fff; 
 
    margin: 0px 20px; 
 
    height: 100px; 
 
} 
 
h2 { 
 
    float: left; 
 
}
<!-- Modal 1 --> 
 
<div id="myModal-1" class="modal"> 
 
    <div class="modal-content"> 
 
    <div class="box-content"> 
 
     <h2>Modal 1</h2> 
 
     <span class="close">&times;</span> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<!-- Modal 2 --> 
 
<div id="myModal-2" class="modal"> 
 
    <div class="modal-content"> 
 
    <div class="box-content"> 
 
     <h2>Modal 2</h2> 
 
     <span class="close">&times;</span> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<button id="modalBtn-1">Modal 1</button> 
 
<button id="modalBtn-2">Modal 2</button>

+0

謝謝@happymacarts,你有什麼建議如何寫它更普遍? – Rob

+0

如果您發現它有用並解決了您的問題,請接受答案。但爲了使其更通用,而不是將事件添加到ID,可以使用類並可能使用數據屬性。 – happymacarts

+0

決定學習Bootstrap並使用它。感謝您的幫助 – Rob