事件目标属性

事件输入的目标属性是对事件被分派到的对象的引用,与在冒泡或者捕获阶段调用事件处理程序时的 Event.currentTarget 相对。

如何使用 JavaScript 创建弹出表单

弹出表单是在上创建对话框的好方法。
我们可以为站点创建弹出式登录表单、联系表单或者任何其他类型的表单。
弹出按钮将位于访问者的眼睛下方。
当用户单击弹出按钮时,表单将出现在屏幕上。

其中我们可以学习如何使用 JavaScript 创建弹出式表单。

对 openForm() 使用 display = "block" 对 closeForm() 函数使用 display = "none" 以在单击时显示和关闭表单:

function openTheForm() {
  document.getElementById("popupForm").style.display = "block";
}
function closeTheForm() {
  document.getElementById("popupForm").style.display = "none";
}

使用 JavaScript 的弹出表单示例:

<!DOCTYPE html>
<html>
  <head>
    <title>文档的标题</title>
    <style>
      * {
        box-sizing: border-box;
      }
      .openBtn {
        display: flex;
        justify-content: left;
      }
      .openButton {
        border: none;
        border-radius: 5px;
        background-color: #1c87c9;
        color: white;
        padding: 14px 20px;
        cursor: pointer;
        position: fixed;
      }
      .loginPopup {
        position: relative;
        text-align: center;
        width: 100%;
      }
      .formPopup {
        display: none;
        position: fixed;
        left: 45%;
        top: 5%;
        transform: translate(-50%, 5%);
        border: 3px solid #999999;
        z-index: 9;
      }
      .formContainer {
        max-width: 300px;
        padding: 20px;
        background-color: #fff;
      }
      .formContainer input[type=text],
      .formContainer input[type=password] {
        width: 100%;
        padding: 15px;
        margin: 5px 0 20px 0;
        border: none;
        background: #eee;
      }
      .formContainer input[type=text]:focus,
      .formContainer input[type=password]:focus {
        background-color: #ddd;
        outline: none;
      }
      .formContainer .btn {
        padding: 12px 20px;
        border: none;
        background-color: #8ebf42;
        color: #fff;
        cursor: pointer;
        width: 100%;
        margin-bottom: 15px;
        opacity: 0.8;
      }
      .formContainer .cancel {
        background-color: #cc0000;
      }
      .formContainer .btn:hover,
      .openButton:hover {
        opacity: 1;
      }
    </style>
  </head>
  <body>
    <h2>Popup Form</h2>
    <p>单击“打开表单”按钮以打开弹出表单。</p>
    <div class="openBtn">
      <button class="openButton" onclick="openForm()">打开表单</button>
    </div>
    <div class="loginPopup">
      <div class="formPopup" id="popupForm">
        <form action="/action_page.php" class="formContainer">
          <h2>Please Log in</h2>
          <label for="email">
            E-mail
          </label>
          <input type="text" id="email" placeholder="Your Email" name="email" required>
          <label for="psw">
            Password
          </label>
          <input type="password" id="psw" placeholder="Your Password" name="psw" required>
          <button type="submit" class="btn">Log in</button>
          <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
        </form>
      </div>
    </div>
    <script>
      function openForm() {
        document.getElementById("popupForm").style.display = "block";
      }
      function closeForm() {
        document.getElementById("popupForm").style.display = "none";
      }
    </script>
  </body>
</html>

也可以通过单击框外的任意位置来分离模式框并关闭它。
为此,我们只需要使用返回触发事件的元素的目标事件属性:

function openForm() {
  document.getElementById("loginPopup").style.display = "block";
}
function closeForm() {
  document.getElementById("loginPopup").style.display = "none";
}
//当用户点击模态框之外的任何地方时,关闭它
window.onclick = function (event) {
  let modal = document.getElementById('loginPopup');
  if (event.target == modal) {
    closeForm();
  }
}

让多个 Popup Forms 使用 data-global 属性并不太难。
data-
属性用于存储页面私有的自定义数据。
其中我们使用数据模式属性:

let modalBtns = [...document.querySelectorAll(".button")];
modalBtns.forEach(function (btn) {
  btn.onclick = function () {
    let modal = btn.getAttribute('data-modal');
    document.getElementById(modal).style.display = "block";
  }
});
let closeBtns = [...document.querySelectorAll(".close")];
closeBtns.forEach(function (btn) {
  btn.onclick = function () {
    let modal = btn.closest('.modal');
    modal.style.display = "none";
  }
});
window.onclick = function (event) {
  if (event.target.className === "modal") {
    event.target.style.display = "none";
  }
}

在一页中设置多个弹出表单的示例:

<!DOCTYPE html>
<html>  <head>
    <title>文档的标题</title>
    <style>
      .modal {
        display: none;
        position: fixed;
        z-index: 8;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgb(0, 0, 0);
        background-color: rgba(0, 0, 0, 0.4);
      }
      .modal-content {
        margin: 50px auto;
        border: 1px solid #999;
        width: 60%;
      }
      h2,p {
        margin: 0 0 20px;
        font-weight: 400;
        color: #999;
      }      span {
        color: #666;
        display: block;
        padding: 0 0 5px;
      }
      form {
        padding: 25px;
        margin: 25px;
        box-shadow: 0 2px 5px #f5f5f5;
        background: #eee;
      }
      input,
      textarea {
        width: 90%;
        padding: 10px;
        margin-bottom: 20px;
        border: 1px solid #1c87c9;
        outline: none;
      }
      .contact-form button {
        width: 100%;
        padding: 10px;
        border: none;
        background: #1c87c9;
        font-size: 16px;
        font-weight: 400;
        color: #fff;
      }
      button:hover {
        background: #2371a0;
      }
      .close {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
      }
      .close:hover,
      .close:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
      }
      button.button {
        background: none;
        border-top: none;
        outline: none;
        border-right: none;
        border-left: none;
        border-bottom: #02274a 1px solid;
        padding: 0 0 3px 0;
        font-size: 16px;
        cursor: pointer;
      }
      button.button:hover {
        border-bottom: #a99567 1px solid;
        color: #a99567;
      }
    </style>
  </head>
  <body>
    <h2>Multiple Popup Forms</h2>
    <p>
      <button class="button" data-modal="modalOne">Contact Us</button>
    </p>
    <p>
      <button class="button" data-modal="modalTwo">Send a Compliant Form</button>
    </p>
    <div id="modalOne" class="modal">
      <div class="modal-content">
        <div class="contact-form">
          <a class="close">&times;</a>
          <form action="/">
            <h2>Contact Us</h2>
            <div>
              <input class="fname" type="text" name="name" placeholder="Full name">
              <input type="text" name="name" placeholder="Email">
              <input type="text" name="name" placeholder="Phone number">
              <input type="text" name="name" placeholder="Website">
            </div>
            <span>Message</span>
            <div>
              <textarea rows="4"></textarea>
            </div>
            <button type="submit" href="/">Submit</button>
          </form>
        </div>
      </div>
    </div>
    <div id="modalTwo" class="modal">
      <div class="modal-content">
        <div class="contact-form">
          <span class="close">&times;</span>
          <form action="/">
            <h2>Complaint form</h2>
            <div>
              <input class="fname" type="text" name="name" placeholder="Full name">
              <input type="text" name="name" placeholder="Email">
              <input type="text" name="name" placeholder="Phone number">
              <input type="text" name="name" placeholder="Website">
            </div>
            <span>Message</span>
            <div>
              <textarea rows="4"></textarea>
            </div>
            <button type="submit" href="/">Submit</button>
          </form>
        </div>
      </div>
    </div>
    <script>
      let modalBtns = [...document.querySelectorAll(".button")];
      modalBtns.forEach(function(btn) {
        btn.onclick = function() {
          let modal = btn.getAttribute('data-modal');
          document.getElementById(modal)
            .style.display = "block";
        }
      });
      let closeBtns = [...document.querySelectorAll(".close")];
      closeBtns.forEach(function(btn) {
        btn.onclick = function() {
          let modal = btn.closest('.modal');
          modal.style.display = "none";
        }
      });
      window.onclick = function(event) {
        if(event.target.className === "modal") {
          event.target.style.display = "none";
        }
      }
    </script>
  </body>
</html>
日期:2020-06-02 22:16:14 来源:oir作者:oir