Can I check if Bootstrap Modal Shown / Hidden?

前端 未结 11 1411
走了就别回头了
走了就别回头了 2020-12-05 12:29

Can I check if Bootstrap Modal currently Shown / Hidden Programatically?

Like bool a = if(\"#myModal\").shown(); ?

I need true/false

相关标签:
11条回答
  • 2020-12-05 13:20

    The best method is given in the docs

    $('#myModal').on('shown.bs.modal', function () {
      // will only come inside after the modal is shown
    });
    

    for more info refer http://getbootstrap.com/javascript/#modals

    0 讨论(0)
  • 2020-12-05 13:22
    alert($('#myModal').hasClass('in'));
    

    It will return true if modal is open

    0 讨论(0)
  • 2020-12-05 13:22

    With Bootstrap 4:

    if ($('#myModal').hasClass('show')) {
        alert("Modal is visible")
    }
    
    0 讨论(0)
  • 2020-12-05 13:25

    its an old question but anyway heres something i used incase someone was looking for the same thing

    if (!$('#myModal').is(':visible')) {
        // if modal is not shown/visible then do something
    }
    
    0 讨论(0)
  • 2020-12-05 13:25
    if($('.modal').hasClass('in')) {
        alert($('.modal .in').attr('id')); //ID of the opened modal
    } else {
        alert("No pop-up opened");
    }
    
    0 讨论(0)
提交回复
热议问题