此处将为大家介绍关于Javascript-显示:“无”不起作用的详细内容,并且为您解答有关js无反应的相关问题,此外,我们还将为您介绍关于Django子HTML模板中的Javascript不起作用、H
此处将为大家介绍关于Javascript - 显示:“无”不起作用的详细内容,并且为您解答有关js无反应的相关问题,此外,我们还将为您介绍关于Django 子 HTML 模板中的 Javascript 不起作用、HTML/JavaScript - 用 JavaScript 替换 onClick()在 HTML 中addEventListener 不起作用、hypeledger 结构 JavaScript 应用程序节点 app.js 不起作用、javascript - scrollToView(false) 不起作用的有用信息。
本文目录一览:- Javascript - 显示:“无”不起作用(js无反应)
- Django 子 HTML 模板中的 Javascript 不起作用
- HTML/JavaScript - 用 JavaScript 替换 onClick()在 HTML 中addEventListener 不起作用
- hypeledger 结构 JavaScript 应用程序节点 app.js 不起作用
- javascript - scrollToView(false) 不起作用
Javascript - 显示:“无”不起作用(js无反应)
如何解决Javascript - 显示:“无”不起作用
我正在尝试编写自己的电子商店(当然是非商业性的),但我在使用 JavaScript 和 display:"none"
时遇到了问题。当我点击登录 <h4>Log in</h4>
acc_drop
将显示更改为阻止,但如果我点击 <span>x</span>
关闭,则没有任何反应。控制台显示 acc_drop display = "none"
。不知道哪里出错了。
谢谢大家的回复。
window.onload = function () {
let acc = document.querySelector(".acc");
let accdrop = document.querySelector(".acc_drop");
let closebtn = document.querySelector(".close");
acc.onclick = function () {
if (accdrop.style.display = "none") {
accdrop.style.display = "block";
}
}
closebtn.onclick = function() {
accdrop.style.display = "none";
console.log(accdrop.style.display);
}
}
.acc{
padding: 10px;
}
.acc .prihlaseni{
color: #6B6B6B;
cursor: pointer;
}
.acc .prihlaseni:hover{
text-decoration: underline;
}
.acc .acc_drop{
display: none;
position: absolute;
z-index: 80000;
top: 80%;
left: 50%;
transform: translate(-50%,-50%);
background-color: #F5F5F5;
Box-shadow: 0 0 5px 0 ;
width: 260px;
height: 300px;
border-radius: 5px;
text-align: center;
}
.acc .acc_drop .close{
position: absolute;
top: 0;
right: 0;
font-size: 11px;
color: #F5F5F5;
background-color: #9e1b1b;
border-radius: 0 5px 0 5px;
padding: 5px;
cursor: pointer;
}
.acc .acc_drop .close:active{
background-color: #5f0606;
}
.acc .acc_drop .login-title{
margin-top: 10px;
font-size: 8px;
}
.acc .acc_drop form{
display: flex;
flex-direction: column;
padding: 10px;
text-align: left;
margin: 0;
}
.acc label{
text-align: left;
font-size: 9px;
font-weight: 400;
}
.acc .acc_drop input{
width: 100%;
height: 20px;
margin-top: 8px;
outline: none;
border: 1px solid #6B6B6B;
border-radius: 3px;
Box-shadow: 0 0 2px #00FFDD inset;
}
.acc .acc_drop .loginform{
margin-top: 5px;
}
.acc .acc_drop input:focus,textarea:focus {
Box-shadow: 0 0 7px #00FFDD;
border: 1px solid #6B6B6B;
}
.acc .acc_drop .reg{
padding: 10px;
}
.acc .acc_drop .accbtn{
height: 20px;
margin-top: 10px;
margin-bottom: 5px;
border: none;
background-color: #6B6B6B;
border-radius: 5px ;
color: #00FFDD;
font-size: 9px;
font-weight: 600;
cursor: pointer;
outline: none;
}
.acc .acc_drop a{
color: #6B6B6B;
text-decoration: underline;
float: right;
font-size: 9px;
}
.acc .acc_drop a:hover{
text-decoration: none;
}
.acc .acc_drop .reg::before{
content: "";
display: block;
border-bottom: 1px solid #6b6b6b86;
width: 100%;
}
.acc .acc_drop .registr p{
margin-top: 10px;
font-size: 10px;
}
.acc .acc_drop .registr button{
height: 20px;
width: 100%;
margin-top: 5px;
border: none;
background-color: #9e1b1b;
border-radius: 5px ;
color: #ffffff;
font-size: 9px;
font-weight: 600;
cursor: pointer;
outline: none;
}
.acc .acc_drop .accbtn:hover{
background-color: #868686;
transition: 1s;
}
.acc .acc_drop .registr button:hover{
background-color: #e41919;
transition: 1s;
}
.acc .acc_drop .accbtn:active{
background-color: #474747;
transition: none;
}
.acc .acc_drop .registr button:active{
background-color: #5a0b0b;
transition: none;
}
<div class="acc"> <h4 class="prihlaseni">Přihlásit se</h4>
<div class="acc_drop">
<span class="close">x</span>
<form action="">
<div class="login-title"><h1>Přihlášení</h1></div>
<div class="loginform mail">
<label>E-mail *</label>
<input class="e-mail" type="text" required>
</div>
<div class="loginform heslo">
<label>Heslo *</label><a href="">Zapomněl(a) jsem heslo</a>
<input class="heslo" type="password" required>
</div>
<button class="accbtn" type="submit">Přihlásit se</button>
</form>
<div class="reg">
<div class="registr">
<p>Ještě nemáte účet?</p>
<button>Zaregistrovat se</button>
</div>
</div>
</div>
</div>
解决方法
问题在于 if
中的 acc.onclick
条件:
acc.onclick = function () {
if (accdrop.style.display = "none") {
accdrop.style.display = "block";
}
}
不是比较,而是分配值:
if (accdrop.style.display = "none")
应该改为:
,
if (accdrop.style.display === "none")
serverless deploy --stage prod
在 closebtn
内。当您单击 close 时,事件会冒泡回来,同时击中两个事件处理程序。您可以通过调用 acc
:
event.stopPropagation()
此外,您应该以不同的方式检查 closebtn.onclick = function() {
accdrop.style.display = "none";
console.log(accdrop.style.display);
event.stopPropagation();
}
中的显示属性值。当您通过 accdrop
验证时,它不会按预期工作,因为您的带有 accdrop.style.display
的 css 类不会触发。它将仅验证 display: none
属性,而不是其计算值:
,
style
也可以尝试仅在 h1
,
window.onload = function () {
let prihlaseni = document.querySelector(".prihlaseni");
let accdrop = document.querySelector(".acc_drop");
let closebtn = document.querySelector(".close");
prihlaseni.onclick = function () {
if (accdrop.style.display === "none") {
accdrop.style.display = "block";
}
}
试试这个:
.acc {
padding: 10px;
}
.acc .prihlaseni {
color: #6B6B6B;
cursor: pointer;
}
.acc .prihlaseni:hover {
text-decoration: underline;
}
.acc .acc_drop {
display: none;
position: absolute;
z-index: 80000;
top: 80%;
left: 50%;
transform: translate(-50%,-50%);
background-color: #F5F5F5;
box-shadow: 0 0 5px 0;
width: 260px;
height: 300px;
border-radius: 5px;
text-align: center;
}
.acc .acc_drop .close {
position: absolute;
top: 0;
right: 0;
font-size: 11px;
color: #F5F5F5;
background-color: #9e1b1b;
border-radius: 0 5px 0 5px;
padding: 5px;
cursor: pointer;
}
.acc .acc_drop .close:active {
background-color: #5f0606;
}
.acc .acc_drop .login-title {
margin-top: 10px;
font-size: 8px;
}
.acc .acc_drop form {
display: flex;
flex-direction: column;
padding: 10px;
text-align: left;
margin: 0;
}
.acc label {
text-align: left;
font-size: 9px;
font-weight: 400;
}
.acc .acc_drop input {
width: 100%;
height: 20px;
margin-top: 8px;
outline: none;
border: 1px solid #6B6B6B;
border-radius: 3px;
box-shadow: 0 0 2px #00FFDD inset;
}
.acc .acc_drop .loginform {
margin-top: 5px;
}
.acc .acc_drop input:focus,textarea:focus {
box-shadow: 0 0 7px #00FFDD;
border: 1px solid #6B6B6B;
}
.acc .acc_drop .reg {
padding: 10px;
}
.acc .acc_drop .accbtn {
height: 20px;
margin-top: 10px;
margin-bottom: 5px;
border: none;
background-color: #6B6B6B;
border-radius: 5px;
color: #00FFDD;
font-size: 9px;
font-weight: 600;
cursor: pointer;
outline: none;
}
.acc .acc_drop a {
color: #6B6B6B;
text-decoration: underline;
float: right;
font-size: 9px;
}
.acc .acc_drop a:hover {
text-decoration: none;
}
.acc .acc_drop .reg::before {
content: "";
display: block;
border-bottom: 1px solid #6b6b6b86;
width: 100%;
}
.acc .acc_drop .registr p {
margin-top: 10px;
font-size: 10px;
}
.acc .acc_drop .registr button {
height: 20px;
width: 100%;
margin-top: 5px;
border: none;
background-color: #9e1b1b;
border-radius: 5px;
color: #ffffff;
font-size: 9px;
font-weight: 600;
cursor: pointer;
outline: none;
}
.acc .acc_drop .accbtn:hover {
background-color: #868686;
transition: 1s;
}
.acc .acc_drop .registr button:hover {
background-color: #e41919;
transition: 1s;
}
.acc .acc_drop .accbtn:active {
background-color: #474747;
transition: none;
}
.acc .acc_drop .registr button:active {
background-color: #5a0b0b;
transition: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="acc">
<h4 class="prihlaseni" onclick=''document.querySelector(".acc_drop").style.display = "block";''>Přihlásit se</h4>
<div class="acc_drop">
<span class="close" onclick=''document.querySelector(".acc_drop").style.display = "none";''>x</span>
<form action="">
<div class="login-title">
<h1>Přihlášení</h1>
</div>
<div class="loginform mail">
<label>E-mail *</label>
<input class="e-mail" type="text" required>
</div>
<div class="loginform heslo">
<label>Heslo *</label><a href="">Zapomněl(a) jsem heslo</a>
<input class="heslo" type="password" required>
</div>
<button class="accbtn" type="submit">Přihlásit se</button>
</form>
<div class="reg">
<div class="registr">
<p>Ještě nemáte účet?</p>
<button>Zaregistrovat se</button>
</div>
</div>
</div>
</div>
我刚刚将 JavaScript
更改为 inline JavaScript
,这解决了问题并为您节省了一些空间:
onclick=''document.querySelector(".acc_drop").style.display = "block";''
和
onclick=''document.querySelector(".acc_drop").style.display = "none";''