我正在尝试使用 innerHTML 显示一个弹出模型(应该是登录表单),我有很多 divs 及其各自的类,但是我的第一个 div 的类,由于某种原因而未被识别。
在我尝试使用此方法弹出之前,
document.querySelector('.caixa-entrar').style.display = "flex";
它起作用了,但是我关闭这些弹出窗口时遇到了很多问题。我的老师建议使用 innerHTML ,因为他说这更容易。至少他向我展示的方式是...
这是我的身体:
<header id="menu">
<button id="cadastrar" class="button-flex-son">Cadastrar</button>
<button id="entrar" class="button-flex-son">Entrar</button>
</header>
<div></div>
caixa的CSS
background-color: rgba(0, 0, 0, 0.8);
width: 100%;
height: 100%;
position: absolute;
top: 0;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
conteudo的CSS
height: 300px;
width: 500px;
background-color: #3E94D1;
text-align: center;
padding: 20px;
position: relative;
border-radius: 4px;
我的JS脚本
const caixa = document.querySelector('div')
document.getElementById('entrar').addEventListener("click", ()=>{
caixa.innerHTML =
'<div class="caixa">' +
'<div class="conteudo">' +
'<div class="close">+</div>' +
//Insira o coisa de imagem aqui depois, arrombado
'<form class="forms" action="">' +
'<input type="email" name="email" placeholder="E-mail" class="inputao">' +
'<input type="password" name="senha" placeholder="Senha" class="inputao">' +
'<button type="submit" class="butao">Entrar</button>' +
'<button type="submit" id="cadastro-butao" class="butao">Cadastro</button>' +
'</form>' +
'</div>' +
'</div>'
});
所有这些工作,但第一个 div 的类却没有。可以使弹出式窗口的模型在页面中心以蓝色显示,其余的背景应更改其颜色
答案 0 :(得分:0)
您不必将所有表单都构建在JS函数中,因为这会降低代码的速度。您的老师欺骗您,使您认为将完整的HTML代码隐藏到JS中并通过addEventListener
参数进行调用会更容易。
您只需在窗体上设置窗体的位置和样式并将其隐藏,然后在“ Entrar”按钮上的onClick
事件上调用它即可,就这么简单。我制作了一个代码段,以便您可以查看是否愿意这样做,也可以查看代码并随时将其实现到您的网站中,在这里就可以了:
var modal = document.getElementById("hiddenForm");
//Oh desgraça, isso aqui é só pra fechar o Modal mesmo quando clicar do lado de fora
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
body {
font-family: Arial, Helvetica, sans-serif;
}
input[type=text],
input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #333;
filter: brightness(100%);
transition: all 0.2s ease-in-out;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
filter: brightness(50%);
}
.cancelbtn {
width: auto;
padding: 10px 18px;
background-color: #500;
}
.container {
padding: 30px;
}
span.pass {
float: right;
padding-top: 16px;
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.4);
padding-top: 60px;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto 15% auto;
border: 1px solid #000;
width: 80%;
}
.close {
position: inherit;
float: right;
top: 0;
margin-right: 20px;
margin-top: 10px;
margin-bottom: 10px;
color: #000;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: red;
cursor: pointer;
}
.animate {
animation: animatezoom 0.3s
}
@keyframes animatezoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
<h2>Try me</h2>
<button onclick="document.getElementById('hiddenForm').style.display='block'" style="width:auto;">Entrar</button>
<div id="hiddenForm" class="modal">
<form class="modal-content animate" action="#">
<span onclick="document.getElementById('hiddenForm').style.display='none'" class="close" title="Close Modal">×</span>
<div class="container">
<label for="uname"><b>E-mail</b></label>
<input type="text" placeholder="E-mail" name="uname" required>
<label for="psw"><b>Senha</b></label>
<input type="password" placeholder="Senha" name="pass" required>
<button type="button">Entrar</button>
<button type="button">Cadastro</button>
<label>
<input type="checkbox" checked="checked" name="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('hiddenForm').style.display='none'" class="cancelbtn">Cancel</button>
<span class="pass">Forgot your <a href="#">password?</a></span>
</div>
</form>
</div>