我有一个引导模式对话框,我想首先显示,然后当用户在页面上单击时,它就消失了。我有以下几点:

$(function () {
   $('#modal').modal(toggle)
});

 <div class="modal" id='modal'>
     <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Error:</h3>
        </div>
        <div class="modal-body">
        <p>Please correct the following errors:</p>
        </div>
     </div>
 </div>

模态最初会显示,但在模态之外单击时不会关闭。此外,内容区域没有灰色。我怎么能得到模态显示最初,然后关闭后,用户点击区域外?我怎么能得到背景是灰色的演示?


当前回答

<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> $(window).load(function(){ $('#myModal').modal('show'); }); $(function () { $('#modal').modal('toggle'); }); </script> </head> <body> <div class="container"> <h2>Modal Example</h2> <!-- Trigger the modal with a button --> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <p>Some text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </body> </html>

其他回答

你可以用;

$('#' + $('.modal.show').attr('id')).modal('hide');

用modal('toggle')代替modal(toggle)

$(function () {
   $('#modal').modal('toggle');
});

你可以看到这篇参考文献,但如果这个链接已被删除,请阅读下面的描述:

用id myModal调用一个模态,只需一行JavaScript代码:

$('#myModal').modal(options)

选项

选项可以通过数据属性或JavaScript传递。对于数据属性,将选项名称附加到data-,如data- background =""。

|-----------|-------------|--------|---------------------------------------------|
| Name      | Type        | Default| Description                                 |
|-----------|-------------|--------|---------------------------------------------|
| backdrop  | boolean or  | true   | Includes a modal-backdrop element.          |
|           | the string  |        | Alternatively, specify static for a         |
|           | 'static'    |        | backdrop which doesn't close the modal      |
|           |             |        | on click.                                   |
|           |             |        |                                             |
| keyboard  | boolean     | true   | Closes the modal when escape key is pressed.|   
|           |             |        |                                             |
| focus     | boolean     | true   | Puts the focus on the modal when initialized|       
|           |             |        |                                             |
| show      | boolean     | true   | Shows the modal when initialized.           |
|-----------|-------------|--------|---------------------------------------------|

方法

异步方法和转换 所有API方法都是异步的,并开始转换。他们返回 在转换开始但结束之前立即发送给调用者。 此外,对转换组件的方法调用也将被删除 忽略了。 有关更多信息,请参阅JavaScript文档。

.modal(选项)

激活你的内容作为一个模式。接受可选的选项对象。

$('#myModal').modal({
   keyboard: false
})

模式(’toggle’)

手动切换一个模式。在模态实际显示或隐藏之前(即在show .bs.modal或hidden.bs.modal事件发生之前)返回调用者。

$('#myModal').modal('toggle')

.modal(显示)

手动打开一个模式。在模态实际显示之前(即在show .bs.modal事件发生之前)返回给调用者。

$('#myModal').modal('show')

模式(’hide’)

手动隐藏模式。在modal实际被隐藏之前(即在hidden.bs.modal事件发生之前)返回给调用者。

$('#myModal').modal('hide')

模式(’handleUpdate’)

手动调整模式的位置,如果模式的高度变化,而它是打开的(即,情况下滚动条出现)。

$('#myModal').modal('handleUpdate')

模式(’dispose’)

破坏元素的模态。

事件

Bootstrap的模态类公开了一些用于连接到模态功能的事件。所有的模态事件都是在模态本身触发的(即在**)。 类型描述

|----------------|--------------------------------------------------------------|
| Event Type     | Description                                                  |
|----------------|--------------------------------------------------------------|
| show.bs.modal  | This event fires immediately when the **show** instance      |
|                | method is called. If caused by a click, the clicked element  |
|                | is available as the **relatedTarget** property of the event. | 
|                |                                                              |
| shown.bs.modal | This event is fired when the modal has been made visible to  |
|                | the user (will wait for CSS transitions to complete). If     |
|                | caused by a click, the clicked element is available as the   | 
|                | **relatedTarget** property of the event.                     |
|                |                                                              |
| hide.bs.modal  | This event is fired immediately when the **hide** instance   |
|                | method has been called.                                      |
|                |                                                              |
| hidden.bs.modal| This event is fired when the modal has finished being hidden |
|                | from the user (will wait for CSS transitions to complete).   |
|----------------|--------------------------------------------------------------|

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})

你可以试试这个

div tabindex="-1"

在你的HTML和jquery你可以使用

$('#myModal').modal('hide')

OR

$('#myModal').modal('toggle')

在我的情况下,主页从引导模式被触发开始不响应后使用$("#modal").modal('toggle');方式,但开始以以下方式回应:

$("#submitBtn").on("click",function(){
  $("#submitBtn").attr("data-dismiss","modal");
});