首页 > 解决方案 > 如何使用 bootstrap 4.4.x 模式从外部页面加载内容

问题描述

我想将外部页面的内容加载到Bootstrap 4.4模式中。这是我的演示

JS:

$('#theModal').on('show.bs.modal', function (e) {

            var button = $(e.relatedTarget);
            var modal = $(this);

            // load content from HTML string
            //modal.find('.modal-body').html("Nice modal body baby...");

            // or, load content from value of data-remote url
            modal.find('.modal-body').load(button.data("remote"));

        });

HTML:

 <ul class="nav flex-row" id="menu">
        <li>
            <a href="/" class="nav-link">Link</a>
        </li>
        <li>
            <a href="/" class="nav-link">Link</a>
        </li>
        <li>
            <a href="#theModal" class="nav-link" data-remote="https://www.lipsum.com" data-toggle="modal" data-target="#theModal">Modal</a>
        </li>
    </ul>

    <div class="modal fade" id="theModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">My modal</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    ... remote content from "data-remote" loads here ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Send message</button>
                </div>
            </div>
        </div>
    </div>

我该如何做到这一点?

标签: bootstrap-4bootstrap-modal

解决方案


最终,我解决了这个问题。在我的身体标签中,我有引导程序的脚本,例如

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
        integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
        crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
        integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
        crossorigin="anonymous"></script>

我所做的是,我删除了

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
        integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
        crossorigin="anonymous"></script>

此脚本条目来自 body 标记,并将 jQuery cdn 链接添加到 header 标记。

这对我来说很有用。


推荐阅读