首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用JavaScript/ jQuery从外部加载Bootstrap 4 Modal的内容

基础概念

Bootstrap 是一个流行的前端框架,用于快速开发响应式和移动优先的网站。Bootstrap 4 是该框架的一个版本,提供了丰富的组件,包括模态框(Modal)。模态框是一种弹出窗口,通常用于显示额外的信息、警告、确认对话框等。

相关优势

  1. 响应式设计:Bootstrap 模态框自动适应不同的屏幕尺寸。
  2. 易于定制:可以通过 CSS 和 JavaScript 进行高度定制。
  3. 丰富的组件:Bootstrap 提供了大量的预定义样式和组件,可以快速构建复杂的 UI。
  4. 兼容性:支持大多数现代浏览器。

类型

Bootstrap 4 模态框主要有以下几种类型:

  1. 基本模态框:最简单的模态框,包含标题、内容和关闭按钮。
  2. 大模态框:用于显示更多内容。
  3. 小模态框:用于显示简短信息。
  4. 登录模态框:用于用户登录。

应用场景

  • 显示警告或确认信息。
  • 用户登录或注册。
  • 显示详细信息或帮助文档。
  • 图片或视频的弹出查看。

加载 Bootstrap 4 Modal 内容的方法

使用 JavaScript 加载

代码语言:txt
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Load Bootstrap Modal Content</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<!-- Button to trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open Modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">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">
                <!-- Content will be loaded 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">Save changes</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
    $(document).ready(function(){
        $('#myModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget); // Button that triggered the modal
            var modal = $(this);
            // Load content from an external URL
            $.get('https://example.com/modal-content.html', function(data) {
                modal.find('.modal-body').html(data);
            });
        });
    });
</script>

</body>
</html>

使用 jQuery 加载

代码语言:txt
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Load Bootstrap Modal Content</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<!-- Button to trigger modal -->
<button type="button" class="btn btn-primary" id="openModalBtn">
    Open Modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">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">
                <!-- Content will be loaded 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">Save changes</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
    $(document).ready(function(){
        $('#openModalBtn').click(function(){
            $('#myModal').modal('show');
            $.get('https://example.com/modal-content.html', function(data) {
                $('#myModal .modal-body').html(data);
            });
        });
    });
</script>

</body>
</html>

可能遇到的问题及解决方法

  1. 内容加载失败
    • 原因:可能是由于网络问题或 URL 错误。
    • 解决方法:检查网络连接和 URL 是否正确。
  • 内容显示不正确
    • 原因:可能是由于 CSS 或 JavaScript 冲突。
    • 解决方法:确保引入的 Bootstrap 和 jQuery 库版本兼容,并检查是否有其他 CSS 或 JavaScript 影响模态框的显示。
  • 模态框无法关闭
    • 原因:可能是由于 JavaScript 错误或事件绑定问题。
    • 解决方法:检查 JavaScript 代码,确保事件绑定正确,并查看浏览器控制台是否有错误信息。

参考链接

通过以上方法,你可以使用 JavaScript 或 jQuery 从外部加载 Bootstrap 4 模态框的内容,并解决可能遇到的问题。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

4分2秒

第二十章:类的加载过程详解/72-何为类的主动使用和被动使用

7分45秒

第二十章:类的加载过程详解/76-类的主动使用4

9分9秒

第二十一章:再谈类的加载器/87-测试不同类使用的类加载器

14分10秒

第二十章:类的加载过程详解/77-类的被动使用

4分58秒

第二十章:类的加载过程详解/78-类的使用介绍

7分59秒

第二十章:类的加载过程详解/73-类的主动使用1

13分53秒

第二十章:类的加载过程详解/74-类的主动使用2

11分42秒

第二十章:类的加载过程详解/75-类的主动使用3

11分50秒

第十八章:Class文件结构/01-JVM中篇内容概述

21分15秒

第十八章:Class文件结构/32-javap主要参数的使用

5分4秒

第十八章:Class文件结构/34-javap使用小结

31分41秒

【玩转 WordPress】腾讯云serverless搭建WordPress个人博经验分享

领券