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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744801530a4594520.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信