Twitter-bootstrap Modal not showing on page-load (

2019-07-16 06:27发布

问题:

I am trying to have a twitter-bootstrap modal dialog box appear on page load for a certain view. I am doing this using the code:

<script>
$(window).load(function(){
        $('#shareModal').modal('show');
    });
</script>

<div class="modal hide fade" id="shareModal">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn">Close</a>
    <a href="#" class="btn btn-primary">Save changes</a>
  </div>
</div>

On page load, instead of the modal popping up, the screen appears "greyed" out (faded) and nothing appears. Anyone have any idea what could be going on? Thank you.

回答1:

Hmmm, I think that you should try this with Bootstrap 3:

<div class = "modal fade" id="shareModal" role = "dialog">
    <div class = "modal-dialog">
        <div class = "modal-content">
            <div class="modal-header">
                <a class="close" data-dismiss="modal">×</a>
                <h3>Modal header</h3>
            </div>
            <div class="modal-body">
                <p>One fine body…</p>
            </div>
            <div class="modal-footer">
                <a href="#" class="btn">Close</a>
                <a href="#" class="btn btn-primary">Save changes</a>
            </div>
        </div>
    </div>
</div>

in your browser's console just type: $('#shareModal').modal(); to test your modal



回答2:

<!-- Modal -->
<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" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        One fine 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><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

JS

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

DEMO