我正在制作一个涉及创建登录系统的网站。对于登录和注册,我想使用模型。当我创建第二个模型时,它仅显示第一个模型上的内容。
这是其外观的一个示例。
https://www.w3schools.com/howto/howto_css_modals.asp

    <!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>The First Model</p>
  </div>
</div>

<button id="myRegisterBtn">Register</button>
<div id="myRegister" class="register">
  <div class="register-content">
    <span class="close">&times;</span>
    <p>Second Model</p>
  </div>
</div>


这是我的JavaScript

// 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() {
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('myRegister');

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

// 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";
    }`enter code here`
}


这是我的CSS

    /* --------------- Code for model ---------*/
    /* 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 */
}

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

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

最佳答案

你们两个模态都保存在名为modal的var中,在这些行中:

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


由于在开始单击按钮之前已经运行了所有代码,因此modal var引用您拥有的第二个模式,因此,每次触发onclick事件之一时,它都会在第二个模式上运行。

我建议您只需更改2个变量的名称,或者最好在事件中使用以下方式引用它们:

btn.onclick = function() {
    var modal = document.getElementById('myRegister');
    modal.style.display = "block";
}

关于javascript - 使两个模型显示在页面上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48569726/

10-12 00:07