css JavaScript不会将显示更改为“无”

6psbrbz9  于 2022-11-26  发布在  Java
关注(0)|答案(2)|浏览(131)

我正在尝试向按钮添加单击事件处理程序,该处理程序将导致它将登录表单的CSS显示属性更改为“none”。
JS:

// make the form disappear when the user clicks initSignUpBtn
const initSignUpBtn = document.querySelector(".btn signup-btn");
initSignUpBtn.addEventListener("click", () =>{
    document.getElementById("signinForm").style.display="none";
});

HTML:

<form class="signin-form" id="signinForm">
    <div class="form-group">
        <input type="email" class="email-input" id="emailInput" placeholder="Email or username">
    </div>
    <div class="form-group">
        <input type="password" class="pass-input" id="passwordInput" placeholder="Password">
    </div>
    <div class="form-check">
        <label class="form-check-label">
            <input class="form-check-input" type="checkbox"> Remember me<a class="need-help" href="#">Need Help?</a>
        </label>
    </div>
    <button type="button" class="btn signin-btn" id="signinbtn">Sign in</button>
    <button type="button" class="btn signup-btn" id="initSignUpBtn">New? Sign up!</button>

</form>

我向按钮添加了一个“click”类型的事件侦听器,并编写了一个匿名函数,将窗体的显示属性更改为“none”,但这不起作用。

p5cysglq

p5cysglq1#

您的querySelector不正确。为什么不使用document.getElementById("initSignUpBtn"),因为它上面有一个ID?如果确实要使用querySelector,它应该是document.querySelector(".btn.signup-btn")document.querySelector(".signup-btn")

kpbwa7wx

kpbwa7wx2#

您可以尝试以下逻辑:

<form class="signin-form" id="signinForm">
    <div class="form-group">
        <input type="email" class="email-input" id="emailInput" placeholder="Email or username">
    </div>
    <div class="form-group">
        <input type="password" class="pass-input" id="passwordInput" placeholder="Password">
    </div>
    <div class="form-check">
        <label class="form-check-label">
            <input class="form-check-input" type="checkbox"> Remember me<a class="need-help" href="#">Need Help?</a>
        </label>
    </div>
    <button type="button" class="btn signin-btn" id="signinbtn">Sign in</button>
    <button type="button" class="btn signup-btn" id="initSignUpBtn">New? Sign up!</button>

</form>

第一次

相关问题