多個按鈕觸發同一個Bootstrap自適應模態窗口


在項目中可能會面對這樣的一個場景:

 

界面上有多個按鈕,我們希望點擊這些按鈕彈出同一個模態窗口,但希望模態窗口的內容是動態生成的,即,點擊每個按鈕彈出的模態窗口內容不同。

 

通常情況下,一個按鈕對應一個模態窗口。

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/jquery-2.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style type="text/css">
        body.modal-open,
        .modal-open .navbar-fixed-top,
        .modal-open .navbar-fixed-bottom {
            margin-right: 0;
        }
        .modal {
            top: 100px;
            bottom: auto;
            padding: 0;
            background-color: #ffffff;
            border: 1px solid #999999;
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 6px;
            -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
            box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
            background-clip: padding-box;
        }
        .modal.container {
            max-width: none;
        }
    </style>
</head>
    <body>
        <div class="content" style="margin-left: 100px;margin-top: 100px;">
            <button class="btn btn-primary btn-lg" data-toggle="modal" href="#full-width">打開模態窗口</button>
        </div>
        <div id="full-width" class="modal container fade" tabindex="-1" style="display: none;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">標題</h4>
            </div>
            <div class="modal-body">
                <p>
                    主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容
                </p>
                
            </div>
            <div class="modal-footer" style="text-align: center;">
                <button type="button" data-dismiss="modal" class="btn btn-default">關閉</button>
            </div>
        </div>
    </body>

 

效果如下:
1

以上,通過data-toggle="modal" href="#full-width"實現模態窗口。


現在,在頁面上存在2個按鈕:

<button class="btn btn-primary btn-lg">打開模態窗口1</button>
<button class="btn btn-primary btn-lg">打開模態窗口2</button>

 

我們希望點擊每個按鈕都彈出id為full-width的模態窗口,但模態窗口的標題為按鈕的文本。

 

於是,需要通過Javascript的API來彈出模態窗口,並且,在彈出之前需要把按鈕的文本賦值給模態窗口的標題。

        $(function() {
            $('.content').on("click", "button", function () {
                $('#full-width .modal-header .modal-title').empty().text($(this).text());
                $('#full-width').modal();
            });
        });

 

完整如下:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/jquery-2.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style type="text/css">
        body.modal-open,
        .modal-open .navbar-fixed-top,
        .modal-open .navbar-fixed-bottom {
            margin-right: 0;
        }
        .modal {
            top: 100px;
            bottom: auto;
            padding: 0;
            background-color: #ffffff;
            border: 1px solid #999999;
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 6px;
            -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
            box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
            background-clip: padding-box;
        }
        .modal.container {
            max-width: none;
        }
    </style>
    <script type="text/javascript">
        $(function() {
            $('.content').on("click", "button", function () {
                $('#full-width .modal-header .modal-title').empty().text($(this).text());
                $('#full-width').modal();
            });
        });
    </script>
</head>
    <body>
        <div class="content" style="margin-left: 100px;margin-top: 100px;">
            <button class="btn btn-primary btn-lg">打開模態窗口1</button>
            <button class="btn btn-primary btn-lg">打開模態窗口2</button>
        </div>
        <div id="full-width" class="modal container fade" tabindex="-1" style="display: none;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">標題</h4>
            </div>
            <div class="modal-body">
                <p>
                    主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容主體內容
                </p>
                
            </div>
            <div class="modal-footer" style="text-align: center;">
                <button type="button" data-dismiss="modal" class="btn btn-default">關閉</button>
            </div>
        </div>
    </body>     

 

效果如下:

2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM