延迟后显示Bootstrap模态

时间:2014-12-10 06:40:25

标签: javascript jquery twitter-bootstrap

如何在2秒后显示一个Bootstrap模态而不通过按钮触发模态?

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:22)

假设您已加载bootstrap js文件,请使用setTimeout()事件。另请查看bootstrap docs

setTimeout(function() {
    $('#myModal').modal();
}, 2000);

答案 1 :(得分:10)

$(window).load(function(){
   setTimeout(function(){
       $('#myModal').modal('show');
   }, 2000);
});

当我喜欢它在页面加载时自动触发时,我就是这样使用它。