Javascript:页面上有多个触发按钮,但是它们都触发了上一个事件

时间:2018-11-27 17:37:41

标签: javascript

我有一个带有多个按钮的网页。这些按钮触发包含内容的模式。每个按钮都与一个具有唯一内容的唯一模式相关联。

问题在于每个按钮都会触发与页面底部最后一个按钮相关联的模式。

每个按钮和模式都有一个唯一的ID,所以我不明白为什么会这样。

这是我用来创建每个按钮的代码。

谢谢您的帮助!

// JS CODE FOR THE FIRST MODAL 

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

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

// Get the <span> element that closes the modal
var span = document.getElementById("close-1");

// When the user clicks 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";
    }
}
// When the user press ESC key, close the modal
window.onkeydown = function( event ) {
    if ( event.keyCode == 27 ) {
        modal.style.display = "none";
    }
};
// JS CODE FOR THE SECOND MODAL 

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

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

// Get the <span> element that closes the modal
var span = document.getElementById("close-2");

// When the user clicks 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";
    }
}
// When the user press ESC key, close the modal
window.onkeydown = function( event ) {
    if ( event.keyCode == 27 ) {
        modal.style.display = "none";
    }
};
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1001; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    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: rgb(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    margin: auto;
    padding: auto;
    width: 60%;
}

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

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
<!-- Trigger/Open The Modal #1 -->
<a id="button-1">Modal #1</a>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span id="close-1" class="close">&times;</span>
    <p>I'm the text of the first modal</p>
  </div>

</div>
<!-- Trigger/Open The Modal #2 -->
<a id="button-2">Modal #2</a>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span id="close-2" class="close">&times;</span>
    <p>I'm the text of the second modal</p>
  </div>

</div>

2 个答案:

答案 0 :(得分:1)

不是因为您错过添加一些事件stopPropagation吗?

(而且我认为给JS Vaviable“ span”命名是一个坏主意,这是html标记的名称)

应该更清楚地以这种方式进行编码(服务器元素发生相同的事件)

    var ActionCloseID = ['modal', 'button', 'close'];
    document.onclick = function(e) {
        if (ActionCloseID.includes(e.target.id) ) {
            e.stopPropagation();                  
            modal.style.display = "none";
        }
    }

答案 1 :(得分:0)

<script>
// JS CODE FOR THE FIRST MODAL

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

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

// Get the <span> element that closes the modal
var span = document.getElementById("close-1");

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

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

// Get the <span> element that closes the modal
var span2 = document.getElementById("close-2");

// When the user clicks 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";
    }else if (event.target == modal2) {
        modal2.style.display = "none";
    }
}
// When the user press ESC key, close the modal
window.onkeydown = function( event ) {
    if ( event.keyCode == 27 ) {
        modal.style.display = "none";
        modal2.style.display = "none";
    }
};

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

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

请像这样更新您的脚本,这应该可以。