简体   繁体   English

页面加载时引导 4 模式没有 jquery?

[英]Bootstrap 4 modal on page load sans jquery?

I am having great difficulty displaying a modal on page load, using Bootstrap 4. Here is the code for the modal...it comes directly from online resources:使用 Bootstrap 4 在页面加载时显示模式非常困难。这是模式的代码......它直接来自在线资源:

</body>

<div class="modal fade" id="cookieModal" style="display: block;>
 <div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <h4 class="modal-title">Modal title</h4>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <p>Modal body content...........</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="button" class="btn btn-primary">Save changes</button>
    </div>
  </div><!-- /.modal-content -->
 </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script src="/public/js/logic.js"></script>

</body>

I thought by adding an "id" name for the modal and using "style="display: block" I could control the display using pure Javascript. For example in my external ("logic") file:我想通过为模态添加一个“id”名称并使用“style =“display:block”我可以使用纯 Javascript 控制显示。例如在我的外部(“逻辑”)文件中:

addEventListener("load", initialize); 


function initialize() {

var _warning = document.getElementById("cookieModal");  //for cookie modal display on page load...
_warning.style.display = "block";  //show the cookie modal...

}

However the modal does not seem to display.但是模态似乎没有显示。 Can anybody give some advice WITHOUT using JQuery...just pure JS would be great.任何人都可以在不使用 JQuery 的情况下给出一些建议......纯 JS 会很棒。 I dislike JQuery and do not use it.我不喜欢 JQuery 并且不使用它。 I thank you in advance.我提前谢谢你。

If i got your question this will work;如果我有你的问题,这将起作用; Using jquery;(sorry for using jquery but this is how the bootstrap is showing to do.)使用 jquery;(很抱歉使用 jquery,但这是引导程序显示的方式。)

 $(document).ready(function() {
    $('#cookieModal').modal('show');
//If you want to hide it automatically after 5 secs.
    setTimeout(function() {
      $("#cookieModal").modal('hide');
    }, 5000);
  });

Bootstrap 4 modals require jQuery. Bootstrap 4 模态需要 jQuery。 Just changing the display property is not enough.仅仅改变显示属性是不够的。

If you don't want to use jQuery, use the latest version of Bootstrap (5.0.0), which does not require jQuery for anything.如果您不想使用 jQuery,请使用最新版本的 Bootstrap (5.0.0),它不需要 jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM