2017-04-18 61 views
0

我試圖創建一個新的和獨特的模態每次我點擊其中一個div。問題是,當我點擊divs時,新的模式被創建,但不會像它應該打開。創建點擊模式

如果點擊其中一個div之前點擊該按鈕,默認模式將正常打開。

HTML

<div class="destaque1">Gráfico 1</div> 
<div class="destaque2">Gráfico 2</div> 
<div class="destaque3">Gráfico 3</div> 
<div class="destaque4">Gráfico 4</div> 

<button id="myBtn">Open Modal</button> 

<div id="myModal" class="modal"> 
    <div class="modal-content"> 
     <span class="close">&times;</span> 
     <p>Some text in the Modal..</p> 
    </div> 
</div> 

CSS

.modal { 
    display: none; 
    position: fixed; 
    z-index: 1; 
    padding-top: 100px; 
    left: 0; 
    top: 0; 
    width: 100%; 
    height: 100%; 
    overflow: auto; 
    background-color: rgb(0,0,0); 
    background-color: rgba(0,0,0,0.4); 
} 

.modal-content { 
    background-color: #fefefe; 
    margin: auto; 
    padding: 20px; 
    border: 1px solid #888; 
    width: 80%; 
} 

.close { 
    color: #aaaaaa; 
    float: right; 
    font-size: 28px; 
    font-weight: bold; 
} 

.close:hover, 
.close:focus { 
    color: #000; 
    text-decoration: none; 
    cursor: pointer; 
} 

SCRIPT

var modal = document.getElementById('myModal'); 

var btn = document.getElementById("myBtn"); 

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

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

span.onclick = function() { 
    modal.style.display = "none"; 
    $('#myModal').remove(); 
    $('.modal-content').remove(); 
    $('.close').remove(); 
} 

window.onclick = function(event) { 
    if (event.target == modal) { 
     modal.style.display = "none"; 
     $('#myModal').remove(); 
     $('.modal-content').remove(); 
     $('.close').remove(); 
    } 
} 

$('.destaque1').click(function(){ 

    $('#myModal').remove(); 
    $('.modal-content').remove(); 
    $('.close').remove(); 

    var divModal = document.createElement('div'); 
    divModal.id = "myModal"; 
    divModal.className = "modal"; 

    var divModalContent = document.createElement('div'); 
    divModalContent.className = "modal-content"; 

    var spanClose = document.createElement('span'); 
    spanClose.className = "close"; 

    var t = document.createTextNode("times;"); 
    spanClose.appendChild(t); 

    document.body.appendChild(divModal); 
    divModal.appendChild(divModalContent); 
    divModalContent.appendChild(spanClose); 

    modal.style.display = "block"; 
    modal.style.backgroundColor = "red"; 

}); 

$('.destaque2').click(function(){ 

    $('#myModal').remove(); 
    $('.modal-content').remove(); 
    $('.close').remove(); 

    var divModal = document.createElement('div'); 
    divModal.id = "myModal"; 
    divModal.className = "modal"; 

    var divModalContent = document.createElement('div'); 
    divModalContent.className = "modal-content"; 

    var spanClose = document.createElement('span'); 
    spanClose.className = "close"; 

    var t = document.createTextNode("times;"); 
    spanClose.appendChild(t); 

    document.body.appendChild(divModal); 
    divModal.appendChild(divModalContent); 
    divModalContent.appendChild(spanClose); 

    modal.style.display = "block"; 
    modal.style.backgroundColor = "blue"; 

}); 

$('.destaque3').click(function(){ 

    $('#myModal').remove(); 
    $('.modal-content').remove(); 
    $('.close').remove(); 

    var divModal = document.createElement('div'); 
    divModal.id = "myModal"; 
    divModal.className = "modal"; 

    var divModalContent = document.createElement('div'); 
    divModalContent.className = "modal-content"; 

    var spanClose = document.createElement('span'); 
    spanClose.className = "close"; 

    var t = document.createTextNode("times;"); 
    spanClose.appendChild(t); 

    document.body.appendChild(divModal); 
    divModal.appendChild(divModalContent); 
    divModalContent.appendChild(spanClose); 

    modal.style.display = "block"; 
    modal.style.backgroundColor = "green"; 

}); 

$('.destaque4').click(function(){ 

    $('#myModal').remove(); 
    $('.modal-content').remove(); 
    $('.close').remove(); 

    var divModal = document.createElement('div'); 
    divModal.id = "myModal"; 
    divModal.className = "modal"; 

    var divModalContent = document.createElement('div'); 
    divModalContent.className = "modal-content"; 

    var spanClose = document.createElement('span'); 
    spanClose.className = "close"; 

    var t = document.createTextNode("times;"); 
    spanClose.appendChild(t); 

    document.body.appendChild(divModal); 
    divModal.appendChild(divModalContent); 
    divModalContent.appendChild(spanClose); 

    modal.style.display = "block"; 
    modal.style.backgroundColor = "yellow"; 

}); 

回答

0

當您刪除#myModal從DOM樹中創建並使用該ID創建一個新元素,則modal不會更新,因此仍然指向已刪除的元素。

我建議你調整你的代碼如下:

  • 您將一切與變量modalspan到一個單獨的函數交易(我稱之爲initModal)。
  • 您製作initModal顯示模式(即設置modal.style.display = 'block')。
  • 您使initModal返回modal,以便在初始化後可以輕鬆地應用背景顏色等自定義設置。
  • 您設置了btn.onclick = initModal

所有這些應用,您可以:

var btn = document.getElementById("myBtn"); 
 

 
btn.onclick = initModal; 
 

 
function initModal() 
 
{ 
 
\t var modal = document.getElementById('myModal'); 
 
    var span = document.getElementsByClassName("close")[0]; 
 

 
    span.onclick = function() { 
 
     modal.style.display = "none"; 
 
     $('#myModal').remove(); 
 
     $('.modal-content').remove(); 
 
     $('.close').remove(); 
 
    } 
 
    
 
    window.onclick = function(event) { 
 
     if (event.target == modal) { 
 
      modal.style.display = "none"; 
 
      $('#myModal').remove(); 
 
      $('.modal-content').remove(); 
 
      $('.close').remove(); 
 
     } 
 
    } 
 
    
 
    modal.style.display = "block"; 
 
    return modal; 
 
} 
 

 
$('.destaque1').click(function(){ 
 

 
    $('#myModal').remove(); 
 
    $('.modal-content').remove(); 
 
    $('.close').remove(); 
 

 
    var divModal = document.createElement('div'); 
 
    divModal.id = "myModal"; 
 
    divModal.className = "modal"; 
 

 
    var divModalContent = document.createElement('div'); 
 
    divModalContent.className = "modal-content"; 
 

 
    var spanClose = document.createElement('span'); 
 
    spanClose.className = "close"; 
 

 
    var t = document.createTextNode("times;"); 
 
    spanClose.appendChild(t); 
 

 
    document.body.appendChild(divModal); 
 
    divModal.appendChild(divModalContent); 
 
    divModalContent.appendChild(spanClose); 
 

 
    initModal().style.backgroundColor = "red"; 
 
}); 
 

 
$('.destaque2').click(function(){ 
 

 
    $('#myModal').remove(); 
 
    $('.modal-content').remove(); 
 
    $('.close').remove(); 
 

 
    var divModal = document.createElement('div'); 
 
    divModal.id = "myModal"; 
 
    divModal.className = "modal"; 
 

 
    var divModalContent = document.createElement('div'); 
 
    divModalContent.className = "modal-content"; 
 

 
    var spanClose = document.createElement('span'); 
 
    spanClose.className = "close"; 
 

 
    var t = document.createTextNode("times;"); 
 
    spanClose.appendChild(t); 
 

 
    document.body.appendChild(divModal); 
 
    divModal.appendChild(divModalContent); 
 
    divModalContent.appendChild(spanClose); 
 

 
    initModal().style.backgroundColor = "blue"; 
 
}); 
 

 
$('.destaque3').click(function(){ 
 

 
    $('#myModal').remove(); 
 
    $('.modal-content').remove(); 
 
    $('.close').remove(); 
 

 
    var divModal = document.createElement('div'); 
 
    divModal.id = "myModal"; 
 
    divModal.className = "modal"; 
 

 
    var divModalContent = document.createElement('div'); 
 
    divModalContent.className = "modal-content"; 
 

 
    var spanClose = document.createElement('span'); 
 
    spanClose.className = "close"; 
 

 
    var t = document.createTextNode("times;"); 
 
    spanClose.appendChild(t); 
 

 
    document.body.appendChild(divModal); 
 
    divModal.appendChild(divModalContent); 
 
    divModalContent.appendChild(spanClose); 
 

 
    initModal().style.backgroundColor = "green"; 
 
}); 
 

 
$('.destaque4').click(function(){ 
 

 
    $('#myModal').remove(); 
 
    $('.modal-content').remove(); 
 
    $('.close').remove(); 
 

 
    var divModal = document.createElement('div'); 
 
    divModal.id = "myModal"; 
 
    divModal.className = "modal"; 
 

 
    var divModalContent = document.createElement('div'); 
 
    divModalContent.className = "modal-content"; 
 

 
    var spanClose = document.createElement('span'); 
 
    spanClose.className = "close"; 
 

 
    var t = document.createTextNode("times;"); 
 
    spanClose.appendChild(t); 
 

 
    document.body.appendChild(divModal); 
 
    divModal.appendChild(divModalContent); 
 
    divModalContent.appendChild(spanClose); 
 

 
    initModal().style.backgroundColor = "yellow"; 
 
});
.modal { 
 
    display: none; 
 
    position: fixed; 
 
    z-index: 1; 
 
    padding-top: 100px; 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    overflow: auto; 
 
    background-color: rgb(0,0,0); 
 
    background-color: rgba(0,0,0,0.4); 
 
} 
 

 
.modal-content { 
 
    background-color: #fefefe; 
 
    margin: auto; 
 
    padding: 20px; 
 
    border: 1px solid #888; 
 
    width: 80%; 
 
} 
 

 
.close { 
 
    color: #aaaaaa; 
 
    float: right; 
 
    font-size: 28px; 
 
    font-weight: bold; 
 
} 
 

 
.close:hover, 
 
.close:focus { 
 
    color: #000; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="destaque1">Gráfico 1</div> 
 
<div class="destaque2">Gráfico 2</div> 
 
<div class="destaque3">Gráfico 3</div> 
 
<div class="destaque4">Gráfico 4</div> 
 

 
<button id="myBtn">Open Modal</button> 
 

 
<div id="myModal" class="modal"> 
 
    <div class="modal-content"> 
 
     <span class="close">&times;</span> 
 
     <p>Some text in the Modal..</p> 
 
    </div> 
 
</div>