开发者问题收集

HTML 和 JavaScript 问题“未捕获的 TypeError:无法在控制台中设置 null 的属性‘textContent’”

2021-02-16
220

我一直在关注这个 YouTube 教程,了解如何创建登录/注册表单,但遇到了一个问题。在编写 JS 代码时,我尝试测试“继续”按钮,但未在输入组中提交任何值,但什么也没发生。所以我去检查控制台,结果出现了这个错误消息, “未捕获的 TypeError:无法将属性“textContent”设置为 null” 。错误发生在“messageElement.textContent = message;”区域附近。任何帮助都将不胜感激。

function setFormMessage(formElement, type, message) {
  const messageElement = formElement.querySelector(".form__message");

  messageElement.textContent = message;
  messageElement.classList.remove("form__message--success", "form__message--error");
  messageElement.classList.add(`form__message--${type}`);
}

function setInputError(inputElement, message) {
  inputElement.classList.add("form__input--error");
  inputElement.parentElement.querySelector(".form__input-error-message").textContent = message;
}

function clearInputError(inputElement) {
  inputElement.classList.remove("form__input--error");
  inputElement.parentElement.querySelector(".form__input-error-message").textContent = "";
}

document.addEventListener("DOMContentLoaded", () => {
  const loginForm = document.querySelector("#login");
  const createAccountForm = document.querySelector("#createAccount");

  document.querySelector("#linkCreateAccount").addEventListener("click", e => {
    e.preventDefault();
    loginForm.classList.add("form--hidden");
    createAccountForm.classList.remove("form--hidden");
  });

  document.querySelector("#linkLogin").addEventListener("click", e => {
    e.preventDefault();
    loginForm.classList.remove("form--hidden");
    createAccountForm.classList.add("form--hidden");
  });

  loginForm.addEventListener("submit", e => {
    e.preventDefault();

    // Perform your AJAX/Fetch login

    setFormMessage(loginForm, "error", "Invalid username/password combination");
  });

  document.querySelectorAll(".form__input").forEach(inputElement => {
    inputElement.addEventListener("blur", e => {
      if (e.target.id === "signupUsername" && e.target.value.length > 0 && e.target.value.length < 10) {
        setInputError(inputElement, "Username must be at least 10 characters in length");
      }
    });

    inputElement.addEventListener("input", () => {
      clearInputError(inputElement);
    });
  });
});
@import url('https://fonts.googleapis.com/css2?family=Belleza&display=swap') * {
  box-sizing: border-box;
}


/*Navugation Bar*/

nav {
  z-index: 1;
  height: 120px;
  background: black;
  box-shadow: grey;
  overflow: hidden;
  background-color: black;
  position: sticky;
  top: 0;
  width: 100%;
}

nav ul {
  float: centre;
  text-align: center;
}

nav ul li {
  display: inline-block;
  line-height: 0px;
  margin: 0px 15px;
  padding: 30px;
}

nav ul li a {
  position: sticky;
  color: grey;
  font-size: 20px;
  text-transform: uppercase;
  padding: 50px;
  text-decoration: none;
  width: 100px;
}

nav ul li a:hover {
  color: white;
  font-size: 30px
}


/*Home Page*/

.header-image {
  padding: 0px;
  position: sticky;
  text-align: center;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: auto;
  margin-top: auto;
  width: 100%;
  background-color: black;
  height: 290px
}

#Home-page {
  font-size: 2em;
  margin: 0;
  margin-bottom: 250px;
  background: url(About\ Page\ Background.png)no-repeat;
  background-position: center;
  background-size: cover;
}

.first-container {
  height: 75hv;
  background: rgb(0, 0, 0, .7);
  border: white 10px solid;
  color: grey;
  padding: 50px;
  margin-top: 20%;
  margin-bottom: 30%;
}

.first-container-h1 {
  color: white;
  text-align: center;
  font-size: 4em;
  border-bottom: 20px solid white;
}

.first-container-h2 {
  color: white;
  text-align: center;
  font-size: 4em;
}

#second-container-main {
  width: 80%;
  margin: auto;
  min-width: 460px;
  margin-top: 5%;
  margin-bottom: 10%;
}

.second-container-title {
  background: white;
  text-align: center;
  font-size: 40px;
  height: 6pc;
  line-height: 90px;
}

.second-container {
  height: 75hv;
  background: rgb(0, 0, 0, .7);
  border: white 2px solid;
  color: grey;
  padding: 50px;
  font-size: 20px;
}

.second-container:hover {
  font-size: 1em;
  color: white;
}

.logo {
  float: center;
  width: 20%;
  float: center;
  text-align: center;
  color: white;
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-top: 100px;
  background-color: black;
  height: 100px;
  border: none
}


/* Footer*/

.footer-wrapper {
  width: 100%;
  margin: 0 auto;
  display: block;
}

footer {
  width: 100%;
  height: 300px;
  float: right;
  text-align: center;
  position: relative;
  bottom: 0;
  background-color: black;
}


/*About Page*/

.About-me-page-header {
  font-size: 1em;
  margin: 0;
  background-position: center;
  background-size: cover;
  background-color: grey;
  position: relative;
  text-align: left;
  padding-top: 50px;
  padding-left: 50px;
  height: 400px;
  border: white 10px solid;
  background-color: rgb(0, 0, 0, .7);
  color: white;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
}

#About-page {
  font-size: 1em;
  margin: 0;
  margin-bottom: 250px;
  background: url(About\ Page\ Background.png)no-repeat;
  background-position: center;
  background-size: cover;
}

#About-page-main {
  width: 80%;
  margin: auto;
  min-width: 460px;
  margin-top: 5%;
  margin-bottom: 10%;
}

.About-page-container {
  height: 75hv;
  background: rgb(0, 0, 0, .7);
  border: white 2px solid;
  color: grey;
  padding: 50px;
  font-size: 1em;
}

.About-page-container:hover {
  font-size: 30px;
  color: white;
}

.about-page-title {
  background: white;
  text-align: center;
  font-size: 40px;
  height: 6pc;
  line-height: 90px;
}

.AB-container-h {
  color: white;
  text-align: center;
}


/* Resources*/

.R-first-container {
  height: 75hv;
  background: rgb(0, 0, 0, .7);
  border: white 10px solid;
  color: grey;
  padding: 50px;
  margin-top: 20%;
  margin-bottom: 30%;
}

.R-first-container-h1 {
  color: white;
  text-align: center;
  font-size: 4em;
}


/*Login Page*/

.About-me-page-header {
  font-size: 1em;
  margin: 0;
  background-position: center;
  background-size: cover;
  background-color: grey;
  position: relative;
  text-align: left;
  padding-top: 50px;
  padding-left: 50px;
  height: 400px;
  border: white 10px solid;
  background-color: rgb(0, 0, 0, .7);
  color: white
}

#About-page {
  font-size: 1em;
  margin: 0;
  margin-bottom: 250px;
  background: url(About\ Page\ Background.png)no-repeat;
  background-position: center;
  background-size: cover;
}

#About-page-main {
  margin: auto;
  min-width: 460px;
  margin-top: 5%;
  margin-bottom: 10%;
}


/* Login in and Sign Up Form*/

#Login-page {
  font-size: 2em;
  margin: 0;
  margin-bottom: 250px;
  background: url(About\ Page\ Background.png)no-repeat;
  background-position: center;
  background-size: cover;
}

#Login-page-main {
  --color-primary-dark: #009579;
  --color-primary-dark: #007f67;
  --color-secondary: #252c6a;
  --color-primary-dark: #cc3333;
  --color-success: #4bb544;
  --color-error: red;
  border-radius: 4px;
  margin: 0;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  max-width: 400px;
  margin: 2rem;
  padding: 5rem;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);
  border-radius: var(--border-radius);
  background-color: rgba(0, 0, 0, .7);
  color: white;
  border: 3px solid white;
  width: 1000px
}

.form--hidden {
  display: none
}

.form>*:firstchild {
  margin-top: 0;
}

.form>*:lastchild {
  margin-bottom: 0;
}

.form__title {
  margin-bottom: 2rem;
  text-align: center;
  font-size: 3rem;
}

.form__message {
  margin-bottom: 1rem;
}

.form__message--success {
  color: var(--color-success);
}

.form__message--error {
  text-align: center;
  color: var(--color-error);
}

.form__input-group {
  margin-bottom: 2rem;
}

input,
select,
textarea {
  color: white;
}

.form__input-error-message {
  color: var(--color-error);
  border-bottom: var(--color-error)
}

.form__input-error-message {
  margin-top: 2rem;
  font-size: 1.5rem;
  color: var(--color-error);
}

.form__button {
  width: 100%;
  padding: 1rem 2rem;
  font-weight: bold;
  font-size: 1.1rem;
  color: white;
  background-color: rgb(0, 0, 0, .7);
  outline: none;
  cursor: pointer;
  border: none;
  border-radius: 20px
}

.form__button:hover {
  background-color: white;
  color: black
}

.form__button:active {
  transform: scale(0.98)
}

.form__text {
  font-size: 20px;
  text-align: center;
  cursor: pointer;
}

.form-text,
.form-textarea {
  border-style: none;
}

.form__link {
  text-decoration: none;
  color: white
}

.form__link:hover {
  text-decoration: underline;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Clarte Mentale - Login</title>
  <link rel="stylesheet" href="Style.css" />
</head>

<body id="Login-page">
  <div class="header-image">
    <a href="Home.html">
      <img src="Website Header.png">
    </a>
  </div>

  <nav>
    <ul>
      <li><a href="Home.html">Welcome</a></li>
      <li><a href="About Page.html">About</a></li>
      <li><a href="Resources.html">Resources</a></li>
      <li><a href="Login.html">Login</a></li>
    </ul>
  </nav>

  <div class="About-me-page-header">
    <h1 style="font-size:48px">Login Page</h1>
    <p style="font-size:35px">
      Contents:
    </p>
    <ul style="font-size:25px">
      <li>Login</li>
      <li>Sign Up</li>
    </ul>
  </div>


  <main id="Login-page-main">
    <div class="container">
      <div class="form-container">
        <!-- Login FormUp Form-->

        <form class="form" id="login">
          <h1 class="form__title">Login</h1>
          <div class="form__messsage form__message--error"></div>
          <div class="form__input-group">
            <input type="text" class="form__input" autofocus placeholder="Username or Email" input style="height:30px;font-size:14pt; border:none;  border-bottom: 4px solid black; background-color: rgba(0,0,0,0); width:100%;">
            <div class="form__input-error-message"></div>
          </div>

          <div class="form__input-group">
            <input type="password" class="form__input" autofocus placeholder="Password" style="height:30px;font-size:14pt; border:none; border-bottom: 4px solid black; background-color: rgba(0,0,0,0); width: 100%">
            <div class="form__input-error-message"></div>
          </div>

          <button class="form__button" type="submit">Continue</button>


          <p class="form__text" style="margin-top: 35px;">
            <a class="form__link" id="linkCreateAccount">Don't have an account? Create account</a>
          </p>
        </form>

        <!-- Sign Up Form-->

        <form class="form  form--hidden" id="createAccount">
          <h1 class="form__title">Create Account</h1>
          <div class="form__messsage form__message--error"></div>
          <div class="form__input-group">
            <input type="text" class="form__input" autofocus placeholder="Username" input style="height:30px;font-size:14pt; border:none;  border-bottom: 4px solid black; background-color: rgba(0,0,0,0); width:100%;">
            <div class="form__input-error-message"></div>
          </div>
          <div class="form__input-group">
            <input type="text" class="form__input" autofocus placeholder="Email" input style="height:30px;font-size:14pt; border:none;  border-bottom: 4px solid black; background-color: rgba(0,0,0,0); width:100%;">
            <div class="form__input-error-message"></div>
          </div>

          <div class="form__input-group">
            <input type="password" class="form__input" autofocus placeholder="Password" style="height:30px;font-size:14pt; border:none; border-bottom: 4px solid black; background-color: rgba(0,0,0,0); width: 100%">
            <div class="form__input-error-message"></div>
          </div>

          <div class="form__input-group">
            <input type="password" class="form__input" autofocus placeholder="Confirm password" style="height:30px;font-size:14pt; border:none; border-bottom: 4px solid black; background-color: rgba(0,0,0,0); width: 100%">
            <div class="form__input-error-message"></div>
          </div>

          <button class="form__button" type="submit">Continue</button>


          <p class="form__text" style="margin-top: 35px;">
            <a class="form__link" id="linkLogin">Already have an account? Sign</a>
          </p>

        </form>

  </main>

  <footer>

    <button class="logo" class="footer-wrapper" onclick="topFunction()" id="myBtn" title="Go to top">
                        <img src="Logo.png">
    
                </button>
  </footer>

  <script src="Javascript.js"></script>
  <script src="Login.js"></script>

</body>

</html>
2个回答

您有 <div class="form__messsage ... "> ,而不是 <div class="form__message ... "> 。修复该问题应该有效。GL。

px1mp
2021-02-16

尝试将 DomContentLoaded 替换为 load

MK.
2021-02-16