从不同按钮切换模式弹出窗口

问题描述 投票:0回答:2

我试图从菜单中的不同按钮打开此弹出窗口,但是,弹出窗口内容将保持不变。

谢谢,巴尼克

function OpenModalT() {
  var modal = document.getElementById('myModalT');
  modal.style.display = "block";
}

function CloseModalT() {
  var modal = document.getElementById('myModalT');
  modal.style.display = "none";
}
<li>
  <a href="#" onclick="OpenModalT();">Button 1</a>

  <div id="myModalT" class="modal">
    <div class="modal-content">
      <span class="close" onclick="CloseModalT();">&times;</span>
      <p>
        <ppopup>
          <br>
          <br>
          <strong>Dummy text Dummy text Dummy Text <br><br> Kind Regards
          </strong>
        </ppopup>
      </p>
    </div>
  </div>
</li>

<li>
  <a href="#" onclick="OpenModalT();">Button 2</a>
</li>

html jquery bootstrap-4 popup
2个回答
2
投票

这是一个示例,您可以使用它拥有任意数量的模态框和按钮:

  • 使用具有唯一ID的模态
  • 使用
    <button type="button" data-modal="#some-modal-id">
    切换模式

const elsModals = document.querySelectorAll(".modal");

const toggleModal = (ev) => {
  const elBtn = ev.currentTarget;
  const elModal = document.querySelector(elBtn.dataset.modal);
  // Close all currently open modals:
  elsModals.forEach(el => {
    if (el !== elModal) el.classList.remove("is-active");
  });
  // Toggle open/close targeted one:
  elModal.classList.toggle("is-active");
};

const elsBtns = document.querySelectorAll("[data-modal]");
elsBtns.forEach(el => el.addEventListener("click", toggleModal));
/*QuickReset*/ * {margin:0; box-sizing: border-box;}

.modal {
  position: fixed;
  z-index: 99999;
  display: flex;
  justify-content: center;
  align-items: center;
  left: 0; top: 0;
  width: 100vw; height: 100vh;
  transition: 0.4s;
  backdrop-filter: blur(3px);
  background: rgba(0,0,0,0.1);
  pointer-events: none;
  visibility: hidden;
  opacity: 0;
}
.modal.is-active {
  pointer-events: auto;
  visibility: visible;
  opacity: 1;
}
.modal-content {
  position: relative;
  background: #fff;
  padding: 30px;
  box-shadow: 0 5px 20px -5px rgba(0,0,0,0.3);
}
.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
}
<div id="modal-regards" class="modal">
  <div class="modal-content">
    <button type="button" data-modal="#modal-regards" class="modal-close">&times;</button>
    <p>Dummy text Dummy text Dummy Text<br>Kind Regards</p>
  </div>
</div>

<div id="modal-help" class="modal">
  <div class="modal-content">
    <button type="button" data-modal="#modal-help" class="modal-close">&times;</button>
    <p>If something is not clear,<br>feel free to ask.</p>
  </div>
</div>

<button type="button" data-modal="#modal-regards">Kind regards</button>
<button type="button" data-modal="#modal-regards">Kind regards</button>
<button type="button" data-modal="#modal-help">Ask for help!</button>

另外:

  • 避免使用内联 on* 处理程序,就像你希望不使用内联样式属性一样 - JS 和 CSS 应该只在一个地方 - 这就是你的文件或标签。
  • 使用更好的代码编辑器。
    "
    不太一样 - 这样的编辑器会标记并突出显示那些小拼写错误。
  • <ppopup>
    是无效的 HTML5 标签。 – Roko C. Buljan 16 分钟前
  • 如果您需要按钮,请勿使用
    <a href="#">
    。请使用
    <button type="button">
    来代替。

0
投票

您需要从

display: none
开始。

var modal = document.getElementById('myModalT');

function OpenModalT() {
  modal.style.display = "block";
}

function CloseModalT() {
  modal.style.display = "none";
}

function ModalFunction() {
  if (modal.style.display === "none") {
    modal.style.display = "block";
  } else {
    modal.style.display = "none";
  }
}
/*Add this:*/

#myModalT {
  display: none;
}
<li>
  <a href="#" onclick="ModalFunction();">Button 1</a>

  <!-- The Modal -->
  <div id="myModalT" class="modal">

    <!-- Modal content -->
    <div class="modal-content">
      <p>
        <ppopup>
          <br>
          <br>
          <strong>Dummy text Dummy text Dummy Text <br><br> Kind Regards
          </strong>
          <span class="close" onclick="ModalFunction();">&times;</span>
        </ppopup>
      </p>
    </div>
  </div>
</li>

<li>
  <a href="#" onclick="ModalFunction();">Button 2</a>
</li>

添加了一些功能:

  1. 您可以再次点击关闭
  2. 节省 JavaScript 空间
© www.soinside.com 2019 - 2024. All rights reserved.