【问题标题】:How do I trigger same popup from different buttons?如何从不同的按钮触发相同的弹出窗口?
【发布时间】:2021-07-12 12:11:07
【问题描述】:

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

谢谢,Banick

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>

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

    <!-- Modal content -->
    <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>

【问题讨论】:

  • PS:避免使用内联 on* 处理程序,就像你希望不要使用内联 style 属性一样——JS 和 CSS 应该只在一个地方——那就是你的文件或标签。跨度>
  • 另外,请安装更好的代码编辑器。 不完全是"
  • &lt;ppopup&gt; 是无效的 HTML5 标记。
  • 如果您需要按钮,请不要使用&lt;a href="#"&gt;。请改用&lt;button type="button"&gt;Click me&lt;/button&gt;
  • 除了@RokoC.Buljan 的不良做法列表之外,实际问题是什么?

标签: html jquery bootstrap-4 popup


【解决方案1】:

你需要以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 中节省空间

【讨论】:

    【解决方案2】:

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

    • 使用具有唯一 ID 的模式
    • 使用&lt;button type="button" data-modal="#some-modal-id"&gt; 切换您的模式

    const EL_modals = document.querySelectorAll(".modal");
    
    const toggleModal = (ev) => {
      const EL_btn = ev.currentTarget;
      const EL_modal = document.querySelector(EL_btn.dataset.modal);
      // Close all currently open modals:
      EL_modals.forEach(EL => {
        if (EL !== EL_modal) EL.classList.remove("is-active");
      });
      // Toggle open/close targeted one:
      EL_modal.classList.toggle("is-active");
    };
    
    const EL_modalBtns = document.querySelectorAll("[data-modal]");
    EL_modalBtns.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 应该只在一个地方 - 那是您的文件或标签。
    • 使用更好的代码编辑器。 " 不太一样 - 这样的编辑器会标记和突出那些小的错别字。
    • &lt;ppopup&gt; 是无效的 HTML5 标记。 – Roko C. Buljan 16 分钟前
    • 如果您需要按钮,请不要使用&lt;a href="#"&gt;。请改用&lt;button type="button"&gt;

    【讨论】:

    • 嗨 Roko,谢谢你的意见,伙计。一点点即兴创作,代码效果很好。由于网站建得有点旧,我已经用列表样式构建了菜单。我想使用 ul li 并通过 onClick 触发模式。但是,该行存在语法错误: const toggleModal = (ev) => { 请提出建议。非常感谢:)
    • @Banick 那是ES6 syntax。要么针对现代浏览器。或者使用 Babel 将 ES6 转换为 ES5(或者使用 Snowpack 构建,它使用了惊人的 esbuild 或 Webpack) - 或者只使用更好的 code editor,它不会争论现代 JS 功能。
    • 如果我必须使用 href 而不是按钮,会有什么变化?谢谢,无论如何我都必须下载新的编辑器,猜猜是时候了:|
    • &lt;a&gt; 又名:Anchors 是指向页面的链接。 &lt;button&gt; 是……正如它所说的那样:按钮。请记住始终使用 type="button" 属性 - 因为默认情况下是 type="submit"
    • 谢谢,虽然我无法单击模态位置下的任何内容。我在导航栏下有一个英雄滑块。
    猜你喜欢
    • 2014-03-16
    • 2021-03-03
    • 2017-10-16
    • 2022-10-23
    • 2020-06-02
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多