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

$(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>

其他回答

这很有效

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

然而,当你有多个情态动词堆叠在另一个上面时,它是无效的,所以,相反,这是有效的

data-dismiss="modal"

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

用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...
})

我用这个技巧以编程方式关闭了模态

在modal中添加一个带有data-dismiss="modal"的按钮,并用display: none隐藏该按钮。这是它看起来的样子

<div class="modal fade" id="addNewPaymentMethod" role="dialog">
  <div class="modal-dialog">
   .
   .
   .
   <button type="button" id="close-modal" data-dismiss="modal" style="display: none">Close</button>
  </div>
</div>

现在,当你想以编程方式关闭模态时,只需在该按钮上触发一个单击事件,这对用户是不可见的

在Javascript中,你可以像这样触发点击按钮:

document.getElementById('close-modal').click();

使用模态。Hide只会隐藏模态。如果你在模态下面使用覆盖,它仍然会在那里。使用点击调用实际关闭模式,并删除覆盖。

$("#modalID .close").click()

此外,你可以“点击”一个“x”,关闭对话框。 例如:

$ (" .ui-dialog-titlebar-close ") .click ();