如何在页面加载时弹出 Bootstrap 模态框?

huangapple go评论46阅读模式
英文:

How to get Bootstrap modal pop up on page load?

问题

我有一个带有基本模态框的静态HTML页面,使用Bootstrap文档中的代码:

<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

在我的app.js文件中,我有一个app.get()方法来提供此文件:

app.get("/", (req, res) => {
    res.sendFile(__dirname + "/modal.html");
});

我想在页面加载时弹出此模态框,我该怎么做?

英文:

I have a static HTML page with a basic modal from Bootstrap docs:

   &lt;div class=&quot;modal fade&quot; id=&quot;exampleModal&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
        &lt;div class=&quot;modal-dialog&quot;&gt;
            &lt;div class=&quot;modal-content&quot;&gt;
                &lt;div class=&quot;modal-header&quot;&gt;
                    &lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;Modal title&lt;/h5&gt;
                    &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
                &lt;/div&gt;
                &lt;div class=&quot;modal-body&quot;&gt;
                    ...
                &lt;/div&gt;
                &lt;div class=&quot;modal-footer&quot;&gt;
                    &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                    &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot;&gt;Save changes&lt;/button&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

In my app.js file I have an app.get() method serving this file:

app.get(&quot;/&quot;, (req, res) =&gt; {
    res.sendFile(__dirname + &quot;/modal.html&quot;);
});

I would like this modal to pop up on page load, how do I do that?

答案1

得分: 1

请尝试使用DOMContentLoaded事件。类似这样的代码:

<script>
    window.addEventListener('DOMContentLoaded', function () {
        var myModal = new bootstrap.Modal(document.getElementById('exampleModal'), {
            keyboard: false
        });
        myModal.show();
    });
</script>

注意: 上面的代码未经测试,但你需要类似这样的代码。

英文:

try using DOMContentLoaded event. Something like this:

&lt;script&gt;
    window.addEventListener(&#39;DOMContentLoaded&#39;, function () {
        var myModal = new bootstrap.Modal(document.getElementById(&#39;exampleModal&#39;), {
            keyboard: false
        });
        myModal.show();
    });
&lt;/script&gt;

NOTE: The above code is not tested but you'd need something like this.

huangapple
  • 本文由 发表于 2023年5月18日 02:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定