简体   繁体   English

如何在 javascript 中加载时在窗口上显示引导模式?

[英]How show bootstrap modal on window on load in javascript?

// I found it in jquery but i want it in javascript is there any alternative code. // 我在 jquery 中找到它,但我想在 javascript 中找到它是否有任何替代代码。 Please help, thanks请帮忙,谢谢

 <script type="text/javascript">

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

 </script>

I believe bootstrap toggles a class on the modal (haven't looked it up recently.我相信引导程序会在模态上切换一个类(最近没有查过它。

The plain JS equivalent would be普通的 JS 等价物是

 window.addEventListener('load', () => { const modal = document.querySelector('#myModal'); if (modal) { modal.classList.add('show'); modal.style.display = 'block'; } });
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" /> <div class="modal fade" id="myModal" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal title</h5> <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 text goes here.</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> </div> </div>

You may need to check the bootstrap docs to be sure what the class name it adds in.您可能需要检查引导程序文档以确保它添加的类名是什么。

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

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