页面加载时自动打开模式

时间:2019-12-25 09:44:51

标签: javascript html modal-dialog

对标题进行更多说明,我有一个模版,该模版来自我所购买的模板,该模版仅真正显示了如何使用按钮打开它,但我希望模版在页面加载时显示。

问题是使用以下代码在页面加载后无法打开:

<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

<script>
            var modal = document.getElementById('myModal');

// Get the <span> element that closes the modal
var span = document.getElementsByClassName('close')[0];

// When the user clicks on the button, open the modal
 $(window).on('load',function(){
  modal.style.display = 'block';
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = 'none';
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = 'none';
  }
}


</script>

1 个答案:

答案 0 :(得分:0)

请尝试此加载页面

<script type="text/javascript">
    $(window).on('load',function(){
        $('#myModal').modal('show');
    });
</script>

如果...则使用此html

<div class="modal hide fade" id="myModal">
  <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>

希望上面的代码对您有所帮助,下面的链接也将对您有帮助

Launch Bootstrap Modal on page load

谢谢