2016-10-10 83 views
1

我很抱歉,如果这是以前的答案,但我一直在盯着这个代码几个小时,无法弄清楚为什么它的行为是这样。多种模式无法正常工作

我有一个页面上有3个模态窗口,我使用javascript来触发。现在只有一个模式窗口它工作正常,但我已经尝试添加两个更多的窗口和第一个窗口的第一个链接触发第三个模式,然后关闭功能不再工作。

这里是我的代码:

CSS:

<style> 
/* The Modal (background) */ 
.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 */ 
} 

.modal1 { 
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 */ 
} 

.modal2 { 
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 { 
background-color: #fefefe; 
margin: 15% auto; /* 15% from the top and centered */ 
padding: 20px; 
border: 1px solid #888; 
width: 80%; /* 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; 
} 

/* Modal Header */ 
.modal-header { 
padding: 2px 16px; 
background-color: #5cb85c; 
color: white; 
} 

/* Modal Body */ 
.modal-body {padding: 2px 16px;} 

/* Modal Footer */ 
.modal-footer { 
padding: 2px 16px; 
background-color: #5cb85c; 
color: white; 
} 

/* Modal Content */ 
.modal-content { 
position: relative; 
background-color: #fefefe; 
margin: auto; 
padding: 0; 
border: 1px solid #888; 
width: 80%; 
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); 
-webkit-animation-name: animatetop; 
-webkit-animation-duration: 0.4s; 
animation-name: animatetop; 
animation-duration: 0.4s 
} 

/* Add Animation */ 
@-webkit-keyframes animatetop { 
from {top: -300px; opacity: 0} 
to {top: 0; opacity: 1} 
} 

@keyframes animatetop { 
from {top: -300px; opacity: 0} 
to {top: 0; opacity: 1} 
} 
</style> 

HTML &的Javascript:

<!-- Trigger/Open The Modal --> 
<a href="#" id="myBtn">Open Modal 1</a> 
<a href="#" id="myBtn1">Open Modal 2</a> 
<a href="#" id="myBtn2">Open Modal 3</a> 

<!-- modal 1 --> 
<script type="text/javascript"> 
      function modalFun(){ 
       // Get the modal 
       var modal = document.getElementById('myModal'); 

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


       // 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() { 
        console.log(modal); 
        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('myModal1'); 

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


       // 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() { 
        console.log(modal1); 
        modal1.style.display = "block"; 
       } 

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

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

        // Get the modal 
       var modal = document.getElementById('myModal2'); 

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


       // 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() { 
        console.log(modal2); 
        modal2.style.display = "block"; 
       } 

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

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

      window.onload=modalFun; 
     </script> 

<!-- Modal 1 --> 
<div id="myModal" class="modal"> 

<!-- Modal content --> 
<div class="modal-content"> 
<div class="modal-header"> 
<span class="close">×</span> 
<h2>Modal Header</h2> 
</div> 
<div class="modal-body"> 
<p>Some text in the Modal Body</p> 
<p>Some other text...</p> 
</div> 
<div class="modal-footer"> 
<h3>Modal Footer</h3> 
</div> 
</div> 

</div> 

<!-- Modal 1 --> 

<!-- Modal 2 --> 

<!-- The Modal --> 
<div id="myModal1" class="modal1"> 

<!-- Modal content --> 
<div class="modal-content"> 
<div class="modal-header"> 
<span class="close">×</span> 
<h2>Modal Header2</h2> 
</div> 
<div class="modal-body"> 
<p>Some text in the Modal Body</p> 
<p>Some other text...</p> 
</div> 
<div class="modal-footer"> 
<h3>Modal Footer</h3> 
</div> 
</div> 

</div> 

<!-- Modal 2 --> 

<!-- Modal 3 --> 

<!-- The Modal --> 
<div id="myModal2" class="modal2"> 

<!-- Modal content --> 
<div class="modal-content"> 
<div class="modal-header"> 
<span class="close">×</span> 
<h2>Modal Header3</h2> 
</div> 
<div class="modal-body"> 
<p>Some text in the Modal Body</p> 
<p>Some other text...</p> 
</div> 
<div class="modal-footer"> 
<h3>Modal Footer</h3> 
</div> 
</div> 

</div> 

<!-- Modal 3 --> 

回答

1

var span = document.getElementsByClassName("close")[0];每次返回文档中的第一个.close。你需要抓住符合你个人模式的.closequerySelector是一个很好的工具。尝试

var span = modal.querySelector('.close'); 

得到正确的。您还正在使用.onclick来设置您的事件处理程序。这是更好地使用addEventListener

span.addEventListener('click', function() { 
    modal.style.display = 'none'; 
}); 

此外,你复制了大量的代码,并改变什么modal指。将该功能移入功能会更好。

function initializeModal(modalID, buttonID) { 
    // Get the modal element 
    var modal = document.getElementById(modalID); 

    // Get the button that opens the modal 
    var btn = document.getElementById(buttonID); 

    // Get the <span> element that closes the modal 
    var span = modal.querySelector('.close'); 

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

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

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

把它放在一起:

function initializeModal(modalID, buttonID) { 
 
    // Get the modal element 
 
    var modal = document.getElementById(modalID); 
 

 
    // Get the button that opens the modal 
 
    var btn = document.getElementById(buttonID); 
 

 
    // Get the <span> element that closes the modal 
 
    var span = modal.querySelector('.close'); 
 

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

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

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

 
window.addEventListener('load', function() { 
 
    initializeModal('myModal', 'myBtn'); 
 
    initializeModal('myModal1', 'myBtn1'); 
 
    initializeModal('myModal2', 'myBtn2'); 
 
});
/* Reuse this class for each modal */ 
 

 
.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 { 
 
    background-color: #fefefe; 
 
    margin: 15% auto; 
 
    /* 15% from the top and centered */ 
 
    padding: 20px; 
 
    border: 1px solid #888; 
 
    width: 80%; 
 
    /* 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; 
 
} 
 
/* Modal Header */ 
 

 
.modal-header { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 
/* Modal Body */ 
 

 
.modal-body { 
 
    padding: 2px 16px; 
 
} 
 
/* Modal Footer */ 
 

 
.modal-footer { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 
/* Modal Content */ 
 

 
.modal-content { 
 
    position: relative; 
 
    background-color: #fefefe; 
 
    margin: auto; 
 
    padding: 0; 
 
    border: 1px solid #888; 
 
    width: 80%; 
 
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); 
 
    -webkit-animation-name: animatetop; 
 
    -webkit-animation-duration: 0.4s; 
 
    animation-name: animatetop; 
 
    animation-duration: 0.4s 
 
} 
 
/* Add Animation */ 
 

 
@-webkit-keyframes animatetop { 
 
    from { 
 
    top: -300px; 
 
    opacity: 0 
 
    } 
 
    to { 
 
    top: 0; 
 
    opacity: 1 
 
    } 
 
} 
 
@keyframes animatetop { 
 
    from { 
 
    top: -300px; 
 
    opacity: 0 
 
    } 
 
    to { 
 
    top: 0; 
 
    opacity: 1 
 
    } 
 
}
<!-- Trigger/Open The Modal --> 
 
<a href="#" id="myBtn">Open Modal 1</a> 
 
<a href="#" id="myBtn1">Open Modal 2</a> 
 
<a href="#" id="myBtn2">Open Modal 3</a> 
 

 
<!-- Modal 1 --> 
 
<div id="myModal" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <div class="modal-header"> 
 
     <span class="close">×</span> 
 
     <h2>Modal Header</h2> 
 
    </div> 
 
    <div class="modal-body"> 
 
     <p>Some text in the Modal Body</p> 
 
     <p>Some other text...</p> 
 
    </div> 
 
    <div class="modal-footer"> 
 
     <h3>Modal Footer</h3> 
 
    </div> 
 
    </div> 
 

 
</div> 
 

 
<!-- Modal 2 --> 
 
<div id="myModal1" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <div class="modal-header"> 
 
     <span class="close">×</span> 
 
     <h2>Modal Header2</h2> 
 
    </div> 
 
    <div class="modal-body"> 
 
     <p>Some text in the Modal Body</p> 
 
     <p>Some other text...</p> 
 
    </div> 
 
    <div class="modal-footer"> 
 
     <h3>Modal Footer</h3> 
 
    </div> 
 
    </div> 
 

 
</div> 
 

 
<!-- Modal 3 --> 
 
<div id="myModal2" class="modal"> 
 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <div class="modal-header"> 
 
     <span class="close">×</span> 
 
     <h2>Modal Header3</h2> 
 
    </div> 
 
    <div class="modal-body"> 
 
     <p>Some text in the Modal Body</p> 
 
     <p>Some other text...</p> 
 
    </div> 
 
    <div class="modal-footer"> 
 
     <h3>Modal Footer</h3> 
 
    </div> 
 
    </div> 
 
</div>

+1

的答案是这是完美的,我不知道为什么我不想使用addEventListener和querySelector。这也解释了关闭函数为什么不能在第三模态上工作(因为它试图关闭第一个模态)。非常感谢! @ mike-c – holodout

+0

@holodout你懂了!很高兴我能帮上忙 :) –

0

你的代码是不正确的,你是在同一个函数重新声明变量。 因为这BTN被绑定到最后一个模态在像BTN,BTN1功能 使用不同的变量结束,BTN2

而且 var span = document.getElementsByClassName("close")[0]; 这将始终被绑定到你将不得不使用不同的第一个ID为关闭按钮太..

我建议你使用自举模式你的代码会变得更加干净 这是不是一个好方法

这里是工作提琴 https://jsfiddle.net/c9ftdgum/

+0

@麦克-C具有更好的办法,你可以看到他的答案也将清除代码 –