1. 程式人生 > >BootStrap 模態框禁用空白處點選關閉

BootStrap 模態框禁用空白處點選關閉

模態框(Modal)是覆蓋在父窗體上的子窗體。通常,目的是顯示來自一個單獨的源的內容,可以在不離開父窗體的情況下有一些互動。子窗體可提供資訊、互動等。bootstrap的模態框在預設情況下,點選其他空白區域(通常是遮罩層),模態框會被關閉,那麼以下方法就是禁止點選其他區域關閉模態框。

$('#myModal').modal({backdrop: 'static', keyboard: false});

backdrop:static時,空白處不關閉.

keyboard:false時,esc鍵盤不關閉.

上述程式碼也用於開啟模態框。

也可以使用以下方法:

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog custom-dialog succ-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4>提示資訊</h4>
      </div>
      <div class="modal-body">
        <div><img src="images/loading.gif"><p><span>投標成功!</span><img src="images/success.png"></p></div>
      </div>      
    </div>
  </div>
</div>

這裡的data-backdrop="static"指定一個靜態的背景,當用戶點選模態框外部時不會關閉模態框。而data-keyboard是指當按下 escape 鍵時關閉模態框,設定為 false 時則按鍵無效。