【问题标题】:How to make click only on modal exterior and not on dialog/content?如何只点击模态外部而不是对话框/内容?
【发布时间】:2018-01-26 00:52:41
【问题描述】:

所以我和我的朋友一起为谷歌浏览器做一个扩展,对于大多数功能(即日历、设置等),我们打开一个模式,这样我们就不必重定向到另一个页面。当您在内容之外单击时,我们正在尝试关闭模式。这是一个小标记屏幕截图,可让您了解我在说什么。

我希望能够通过在白色区域之外单击来关闭模式。目前,即使我在白色区域内单击它也会关闭。我已经尝试了几乎所有方法,包括 stopPropagation()pointer-events:none; 和 jquery 禁用。他们都没有因为我不知道的原因而工作。我猜 Chrome 扩展程序有些奇怪。 到目前为止,这是我的一些代码:

    function calendar() {
      document.getElementById("calendmodal").style.display = "block";
    }

document.addEventListener("DOMContentLoaded", function () {
  document.getElementById("calendicon").addEventListener("click", calendar);
  document.getElementById("calendmodal").addEventListener("click", closeModal);
  document.getElementById("calendmodal").addEventListener("click", function(event) {
    event.stopPropagation();
  });  
});

还有 HTML:

<div class="icons" id="icons_calendar">
        <img src='https://preview.ibb.co/jE76Bm/calendar.png' alt="calendar" border="0" id="calendicon"/>
      </div>
    <div id=calendmodal class="generalmodal">
      <div class="modal-content" id="calendcontent">
        <iframe src="https://calendar.google.com/calendar/embed?src=googleapps.wrdsb.ca_ptpdj55efmhg1nb89ruc15fi3k%40group.calendar.google.com&ctz=America%2FToronto" style="border: 0" width="800" height="600" frameborder="0" scrolling="no" visibility="hidden" id="calendiframe"></iframe>
        <p id=infostuff>For a more extensive calendar and<br>more school tools, visit <a href="http://jam.wrdsb.ca">The SJAM Website</a>.</p>
      </div>
    </div>

不太确定我做错了什么,我不知道如何在扩展中做到这一点。

【问题讨论】:

    标签: javascript html css google-chrome-extension event-bubbling


    【解决方案1】:

    我不知道你是如何显示你的模态对话框的,但我对这个问题的处理方法是这样的(你可以根据你的具体情况进行调整):

    我有一个透明的 &lt;div&gt; 作为容器,它占据了整个屏幕,并在其中使用 CSS 定位模式对话框。比如:

    <div id="container" style="position:fixed;top:0;bottom:0;left:0;right:0;display:none">
        <div id="myModal" style="position:absolute;width:300px;height:200px;left:100px;top:100px">
            <!-- Here goes my content -->
        </div>
    </div>
    

    使用这种方法,要显示对话框,我会执行以下操作:

    function openModal(){
      document.getElementById("container").style.display = "block";
    }
    

    当点击透明背景而不是我的对话框时关闭它:

    document.getElementById("container").onclick = function(event) {
      if (event.target === this) {
        this.style.display = "none";
      }
    };
    

    【讨论】:

    • 我不能使用上面的格式,因为它在扩展中不起作用。我必须在DOMContentLoaded 函数下添加一个事件监听器。
    • 好的,我稍微使用了你的代码,但我对其进行了一些调整。我阅读了很多关于事件侦听器的内容并制作了 document.getElementById("calendmodal").addEventListener("click", function(event){if (event.target !== this)return; document.getElementById("calendmodal").style.display = "none";}); 我会将其标记为正确,因为它确实对我有所帮助。
    • @RohanShetty 您可以使用更短的代码完成相同的操作:document.getElementById("calenmodal").addEventListener("click", function(event){if (event.target === this) this.style.display = "none";});
    【解决方案2】:

    在您之前的尝试中,您在哪里执行了event.stopPropagation()?它不包含在您的代码示例中。

    您需要做的是防止#calendmodal 上的click 事件冒泡DOM 并触发body(或另一个容器元素)上的点击事件,其事件处理程序会关闭您的模态对话框。

    你可以这样做:

    document.getElementById("calendmodal").addEventListener("click", function(event) {
        event.stopPropagation();
    });
    

    【讨论】:

    • 我之前试过event.stopPropagation但是没用,所以我暂时删除了。我会尝试这种格式。
    • 我试过你说的,但现在无论我点击哪里都不会关闭模态框。
    • @RohanShetty 您很可能在错误元素的事件处理程序中使用它。 event.stopPropgation() 必须在 click 事件处理程序中为 #calendmodal 调用
    • @RohanShetty 请更新原始问题中的代码示例。如果您不提供您编写的代码,我无法提供建议。
    • 更新了代码。你是说它需要在函数calendar内?
    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 2011-08-04
    • 2019-12-11
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多