gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:14:15 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com