javascript - 让两个模型显示在页面上

标签 javascript html css

我正在制作一个涉及创建登录系统的网站。对于登录和注册,我想使用模型。当我去创建我的第二个模型时,它只显示第一个模型上的内容。
这是它的外观示例。
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 modal = document.getElementById('myModal');
var modal = document.getElementById('myRegister');

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

我建议您简单地更改这两个变量的名称,或者更好地在您的事件中引用它们,这样:

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

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

相关文章:

javascript - 在 Internet Explorer 中交换选项框?

javascript - 为什么 CSS 居中会弄乱 Canvas 鼠标坐标?

javascript - 在网站加载完成之前,侧边栏不会显示在正确的位置

css - 方向改变时如何将多个绝对位置div更改为手机屏幕的中心?

html - 当 HTML 缩小时,是否有一种有效的方法可以在列表项之间强制使用空格?

javascript - 使用javascript自动开始打印html页面

javascript - 如何在 NodeJS 中将音乐播放器同步到多个客户端

javascript - 如何在 Javascript 中创建带有图像的旋转轮?

html - 响应式全宽 Bootstrap 轮播,左侧为小图像,右侧为标题

javascript - 即使在输入正确的值后 HTML 表单仍显示错误