最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bootstrap modal: close modal and remove from DOM - Stack Overflow

programmeradmin3浏览0评论

I have a page where I want to close a bootstrap 3 modal and then remove the modal itself from the DOM.

So, I've tried to do it this way:

let modal = $('#myModal');
modal.modal('hide');
modal.remove();

The problem is that this solution closes the modal popup itself but leaves the darkened semi-transparent background over the page. I suspect this is because the modal gets removed from the page before the closing animation pletes.

I know I can just set an timer to wait a bit and ensure that the modal has closed before removing it from the DOM, but what I wanted to know is: is there a more "proper" way that doesn't rely on an arbitrary timer?

I have a page where I want to close a bootstrap 3 modal and then remove the modal itself from the DOM.

So, I've tried to do it this way:

let modal = $('#myModal');
modal.modal('hide');
modal.remove();

The problem is that this solution closes the modal popup itself but leaves the darkened semi-transparent background over the page. I suspect this is because the modal gets removed from the page before the closing animation pletes.

I know I can just set an timer to wait a bit and ensure that the modal has closed before removing it from the DOM, but what I wanted to know is: is there a more "proper" way that doesn't rely on an arbitrary timer?

Share Improve this question asked Mar 20, 2018 at 9:19 Master_TMaster_T 8,06116 gold badges90 silver badges171 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can try this code.

Bootstrap 3

$('#myModal').on('hidden.bs.modal', function () {
    $('#myModal').remove();
});

Bootstrap 2.3.2

$('#myModal').on('hidden', function () {
    $('#myModal').remove();
});

The event will be triggered after modal close.

发布评论

评论列表(0)

  1. 暂无评论