【问题标题】:Bootstrap modal won't open on page load引导模式不会在页面加载时打开
【发布时间】:2019-01-01 01:34:07
【问题描述】:

我有一个通过按钮成功打开的模式:

<button onclick="content()" data-toggle="modal" data-target="#my_modal">Save</button>

<form action="addPage.php" method="post">
  <div class="modal fade" id="my_modal" tabindex="-1" role="dialog" aria-labelledby="savePageLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="savePageLabel">Page Details:</h5>
        </div>
      </div>
    </div>
  </div>
</form>

但我试图在页面加载时打开它,但它不起作用:

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

我觉得它应该是正确的语法和一切,但我不明白为什么按钮会打开它但页面加载和 document.ready 不会?

更新:

我的包括

  <script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
  <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script type="text/javascript" src="instafeed.js-master/instafeed.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.0/moment-with-locales.js"></script>
  <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  <script src="https://rawgit.com/tempusdominus/bootstrap-4/master/build/js/tempusdominus-bootstrap-4.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
    <script type='text/javascript' src='//cdn.jsdelivr.net/jquery.marquee/1.4.0/jquery.marquee.min.js'></script>

【问题讨论】:

  • 你是否包含了 jquery 库文件?您使用的是哪个引导程序版本?
  • 刚刚更新了我所有的 javascript 包括。使用引导程序 4.0.0。我的其他 javascript 函数正在运行
  • 您是否在控制台中遇到任何问题?

标签: javascript jquery bootstrap-4 pageload


【解决方案1】:

即使 ID 错误(大写 M),您的代码也能正常工作。

查看您的代码是否正常工作 (chrome)...https://jsfiddle.net/joshmoto/njd7vuLx/

你加载引导js并正确查询了吗?

您的代码...

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

使用您的原始代码为我工作的 jsfiddle 的屏幕截图。

【讨论】:

  • 无论ID如何,我仍然无法获得它。你能在我的包含中看到问题吗?
  • 就像我说的,我只是在 jsfiddle 中使用了您的代码和 bootstrap 4 和 jquery 框架。它可以工作,因此您的身边可能存在错误,或者您没有正确加载 jquery 或 js。您的页面上还有其他引导 js 组件吗?
  • 我正在使用 chrome,并且模态在 jsfiddle 中为我启动。 imgur.com/a/wtVejF1
  • 其他一切,bootstrap 和 JS,都可以正常工作。但这很奇怪,因为当我加载你的小提琴时它也没有显示
  • 你用什么浏览器?
【解决方案2】:

您使用的 ID 错误。 试试这个:

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

【讨论】:

  • 啊,我没听明白,但不幸的是它仍然无法正常工作
  • 有时“$(window).load”不能正常工作。你确定元素在那里,在代码运行时?
  • 那也不行。一定有什么奇怪的事情发生了
【解决方案3】:

有一个拼写错误

它的“#my_modal”不是“#my_Modal”

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

【讨论】:

  • 由于某种原因仍未加载。我的包含顺序有问题吗?
  • 尝试将 teather.min.js 放在 jquery-3.3.1.min.js 下面,并尝试“on”函数,如 $(document).on('load', function(){ $ ('#my_modal').modal('show'); });
  • 好的,所以我在每个页面上都包含了我的 menu.php 文件。当我删除 它可以工作
【解决方案4】:

试试这个....

$(window).on('load',function(){
        $('#my_modal').modal('show');
    });
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://rawgit.com/tempusdominus/bootstrap-4/master/build/js/tempusdominus-bootstrap-4.min.js"></script>

<button onclick="content()" data-toggle="modal" data-target="#my_modal">Save</button>

<form action="addPage.php" method="post">
  <div class="modal fade" id="my_modal" tabindex="-1" role="dialog" aria-labelledby="savePageLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="savePageLabel">Page Details:</h5>
        </div>
      </div>
    </div>
  </div>
</form>
But I'm trying to have it open on page load instead and it won't work:

【讨论】:

  • 控制台是否出现错误,防火墙是否允许CDN尝试打开code.jquery.com/jquery-3.3.1.min.js
  • 不,但我可能发现了问题。我正在使用 startblocks 和 endblocks 来使用包含的菜单对页面进行模板化。当我删除它们时它会起作用
猜你喜欢
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 2021-11-30
相关资源
最近更新 更多