【问题标题】:Show popup after removing div [duplicate]删除 div 后显示弹出窗口 [重复]
【发布时间】:2017-05-19 09:52:57
【问题描述】:

我想先删除 div,然后显示我的弹出窗口。但是在显示警报时,我可以看到 div。 关闭弹出窗口后,div 将被删除。

$("#btnRemoveDiv").on("click",function(){$("#divRemove").remove();
alert("Removed");});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div id="divRemove">Remove this div first then show the popup. Dont show this while the popup is opened!</div>
<input type="button" id="btnRemoveDiv" value="Remove">

@quirimmo 请看这个:

【问题讨论】:

  • 嗨 Divya,请添加您的 jsfiddle 编辑您的问题,这样更容易检查阅读问题的人
  • 看起来对我有用(Edge 和 Firefox)。
  • 也在 chrome 上工作。
  • 对我来说它也不适用于 chrome

标签: javascript jquery


【解决方案1】:

使用when 将其作为承诺进行管理,它应该可以工作:

$("#btnRemoveDiv").on("click",function(){
  $.when($('#divRemove').remove()).then(function() {alert('removed');});
});
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<div id="divRemove">Remove this div first then show the popup. Dont show this while the popup is opened!</div>
<input type="button" id="btnRemoveDiv" value="Remove">

JSfiddle:

https://jsfiddle.net/91x9ta3n/12/

【讨论】:

  • 不,这不适用于 chrome v58.0.3029.110。
  • 无法在 chrome 中工作 :(
  • 你能在这里试试 jsfiddle 而不是 sn-p 吗? jsfiddle 似乎工作
  • jsfiddle 工作正常
  • 是的,这里也一样。 @divya我有你相同版本的chrome,jsfiddle工作正常。 sn-p 不是。也许是 sn-p 功能中的某些原因
【解决方案2】:

使用超时,以便警报和删除不会同时执行。

$("#btnRemoveDiv").on("click", function () {
    $("#divRemove").remove();
    setTimeout(function () {
        alert("Removed");
    }, 500);
});

jsfiddle:https://jsfiddle.net/91x9ta3n/7/

【讨论】:

  • 如果你想要这个解决方案,只需设置一个最小间隔的超时,4ms,不需要半秒的延迟
  • 超时不是解决办法
  • 另外,我认为您的setTimout中有错字
  • @divya - 你想要达到什么目的?因为如果remove()alert 弹出之前未运行div,您将看到div,因为alert 强制浏览器读取消息
  • 超时是唯一的解决方案,因为警报停止浏览器会重新绘制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多