页面加载时Bootstrap模式打开

Pau*_*aul 9 modal-dialog twitter-bootstrap bootstrap-modal

我有一个带有bootstrap模式的页面(手册:http://getbootstrap.com/javascript/#modals).

我想在页面加载时打开这个模态.然而,这并没有发生.

<!-- Modal -->
<div class="modal fade" id="memberModal" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
  <div class="modal-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 class="modal-title" id="memberModalLabel">Thank you for signing in!</h4>
      </div>
      <div class="modal-body">
        <p>However the account provided is not known.<BR>
        If you just got invited to the group, please wait for a day to have the database synchronized.</p>

        <p>You will now be shown the Demo site.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
<!--
$('#memberModal').modal('show');
//-->
</script>
Run Code Online (Sandbox Code Playgroud)

现在,如果我在代码中添加一个按钮.按下按钮时,模态打开.

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#memberModal">
  Launch modal
</button>
Run Code Online (Sandbox Code Playgroud)

我尝试了几件事,但是我无法让它在页面加载时打开模态.我在html头中调用了bootsrap的js和css.

<!-- Bootstrap - Latest compiled and minified CSS -->
<link rel="stylesheet" type='text/css' href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

<!-- Bootstrap - Latest compiled and minified JavaScript -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

hax*_*tbh 25

document.ready()在您的通话中使用活动.

$(document).ready(function () {

    $('#memberModal').modal('show');

});
Run Code Online (Sandbox Code Playgroud)

jsFiddle更新 - http://jsfiddle.net/uvnggL8w/1/


Pau*_*aul 8

我发现了这个问题.此代码放在一个单独的文件中,该文件是使用php include()函数添加的.这包括在加载Bootstrap文件之前发生的事情.因此Bootstrap JS文件尚未加载,导致此模式无法执行任何操作.

使用上面的代码样本没有错,并且当放置在html页面的正文部分时按预期工作.

<script type="text/javascript">
$('#memberModal').modal('show');
</script>
Run Code Online (Sandbox Code Playgroud)

  • 这就是你应该在你的电话周围使用`document.ready()`事件的原因. (3认同)