Bootstrap: 如何在模态中添加 <br> 标签

Bootstrap: how to add <br> tag in modal

大家。

我正在使用 bootstrap 模式。我打算在模态中设计一个登录功能。

有数据查询功能。但是,'br'这个词直接显示在html中。我只想要一个换行效果。当用户名为空且密码小于6个字母时。

我的html节目:

用户名不能为空<br>密码至少6个字母

没有换行符。

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Website">
    <meta name="keywords" content="HTML, CSS, JavaScript">
    <meta name="author" content="Tom Fan">
    <!-- Bootstrap CSS -->

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=League+Gothic&family=Noto+Sans+TC:wght@100;300;400;500;700;900&family=Open+Sans&family=Roboto:ital,wght@0,400;0,500;0,700;0,900;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <link rel="stylesheet" href="reset.css">
    <link rel="stylesheet" href="style.css">
    
  </head>

  <body>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Login in
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <p class="modal-title" id="exampleModalLabel">Login In</p>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">

        <form>
        
          <label for="user_id">User Name</label>
          <input type="text" id="user_id">
          
          <label for="password">Passowrd</label>
          <input type="password" id="password">
          
          <div class="errorMessage"></div>

        <div class="modal-footer">
          <button type="button submit" class="btn btn-primary">Login in</button>
        </div>
      </form>

      </div>
      
    </div>
  </div>
</div>

<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: Bootstrap Bundle with Popper --->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
-->
<script src="function.js"></script>
</body>
</html>
.error {
    border: 3px solid red;
}

.errorMessage {
    margin: 0;
    margin-bottom: 20px;
    width: 100%;
    color: red;
}
const loginForm = document.querySelector('form');
const userName = document.querySelector('#user_id');
const userPassword = document.querySelector('#password');
const errorDiv = document.querySelector('.errorMessage');

loginForm.addEventListener('submit',(e)=>{
   e.preventDefault;
   console.log(userName.value);
   console.log(userPassword.value);
    //value check
    let errorMessage = ""
    let error = false

    if(!userName.value.length)
    {
        errorMessage += 'User Name can not be empry<br>'
        error= true
    }
    if(error)
    {
        loginForm.user_id.classList.add('error')
        errorDiv.innerText = errorMessage
    }
    else
    {
        loginForm.user_id.classList.remove('error')
        errorDiv.innerText = ''
    }
    if(userPassword.value.length < 6)
    {
        errorMessage += 'At least 6 letters for password'
        error= true
    }
    if(error)
    {
        loginForm.password.classList.add('error')
        errorDiv.innerText = errorMessage
    }
    else
    {
        loginForm.password.classList.remove('error')
        errorDiv.innerText = ''
    }
});

errorDiv.innerText = errorMessage

您正在更改文本,而不是 HTML。将其更改为:

errorDiv.innerHtml = errorMessage