【问题标题】:Multiple Bootstrap modals with different buttons link to respective content not working具有不同按钮的多个引导模式链接到各自的内容不起作用
【发布时间】:2016-06-11 20:50:18
【问题描述】:

如您所见,我是 Stack 新手,如有任何错误请见谅。

我正在为我的朋友在 Bootstrap3 中创建一个商业网站,在我们的(公司)团队页面上,我们有很多成员,所以我想让每个成员在带有按钮的模式中获得更多详细信息。

我使用了来自 w3c 网站的模态示例代码,现在我在不同的成员配置文件框中使用它时对其进行了修改,只有第一人称模态有效,其余模态无效。

这里只有一个模态工作代码。 CSS

<style>
    /* The Modal (background) */
    .modal {
        display: none; /* Hidden by default */
        position: absolute; /* Stay in place */
        z-index: 1; /* Sit on top */
        padding-top: 50px; /* Location of the box */
        left: 0;
        top: 0;
        width: 100%; /* Full width */
        height: 100%; /* Full height */
        overflow: auto; /* Enable scroll if needed */
        background-color: rgb(0,0,0); /* Fallback color */
        background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    }

    /* Modal Content */
    .modal-content {
        position: relative;
        background-color: #fefefe;
        margin: auto;
        padding: 0;
        border: 1px solid #888;
        width: 98%;
        box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
        -webkit-animation-name: animatetop;
        -webkit-animation-duration: 0.4s;
        animation-name: animatetop;
        animation-duration: 0.4s
    }

    /* Add Animation */
    @-webkit-keyframes animatetop {
        from {top:-300px; opacity:0} 
        to {top:0; opacity:1}
    }

    @keyframes animatetop {
        from {top:-300px; opacity:0}
        to {top:0; opacity:1}
    }

    /* The Close Button */
    .close {
        float: right;
        font-size: 28px;
        font-weight: bold;
    }

    .close:hover,
    .close:focus {
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }

    .modal-header {
        padding: 10px 2px 0px 2px;
        font-size: 20px;
        font-weight: bold;

    }

    .modal-body {padding: 2px 2px;}

    }
</style>

HTML

<div class="col-md-3 col-sm-6">
                        <div class="center">
                            <div class="panel panel-default">
                                <div class="ourteam-image">
                                <img src="images/portfolio/thumb/person1.gif" alt="">
                                </div>
                                <div class="panel-body">
                                <h4>Person 1</h4>
                                <h5>Director</h5>
                                <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
                                <!-- Trigger/Open The Modal -->
                                <button type="button" class="btn btn-info btn-sm" id="myBtn" data-toggle="modal" data-target="#myModal1">Open Modal</button>

                                <!-- The Modal -->
                                <div id="myModal1" class="modal">

                                  <!-- Modal content -->
                                  <div class="modal-content">
                                    <div class="modal-header">
                                    <span class="close">×</span>
                                    <p>Person 1</p>
                                    </div>
                                    <div class="modal-body">
                                    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
                                    </div>
                                  </div>
                                </div>
                                </div>                  
                            </div><!--/panel-->
                        </div><!--/center-->
                    </div><!--/col-->

                    <div class="col-md-3 col-sm-6">
                        <div class="center">
                            <div class="panel panel-default">
                                <div class="ourteam-image">
                                <img src="images/portfolio/thumb/person2.gif" alt="">
                                </div>
                                <div class="panel-body">
                                <h4>Person 2</h4>
                                <h5>Director</h5>
                                <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
                                <!-- Trigger/Open The Modal -->
                                <button type="button" class="btn btn-info btn-sm" id="myBtn" data-toggle="modal" data-target="#myModal2">Open Modal</button>

                                <!-- The Modal -->
                                <div id="myModal2" class="modal">

                                  <!-- Modal content -->
                                  <div class="modal-content">
                                    <div class="modal-header">
                                    <span class="close">×</span>
                                    <p>Person 2</p>
                                    </div>
                                    <div class="modal-body">
                                    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
                                    </div>
                                  </div>
                                </div>
                                </div>                  
                            </div><!--/panel-->
                        </div><!--/center-->
                    </div><!--/col-->

                    <div class="col-md-3 col-sm-6">
                        <div class="center">
                            <div class="panel panel-default">
                                <div class="ourteam-image">
                                <img src="images/portfolio/thumb/person3.gif" alt="">
                                </div>
                                <div class="panel-body">
                                <h4>Person 3</h4>
                                <h5>Director</h5>
                                <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
                                <!-- Trigger/Open The Modal -->
                                <button type="button" class="btn btn-info btn-sm" id="myBtn" data-toggle="modal" data-target="#myModal3">Open Modal</button>

                        <!-- The Modal -->
                                <div id="myModal3" class="modal">

                                  <!-- Modal content -->
                                  <div class="modal-content">
                                    <div class="modal-header">
                                    <span class="close">×</span>
                                    <p>Person 3</p>
                                    </div>
                                    <div class="modal-body">
                                <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae.</p>
                                    </div>
                                  </div>
                                </div>
                                </div>                  
                            </div><!--/panel-->
                        </div><!--/center-->
                    </div><!--/col-->

脚本

// Get the modal
    var modal = document.getElementById('myModal1','myModal2','myModal3');

    // Get the button that opens the modal
    var btn = document.getElementById("myBtn");

    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];

    // When the user clicks the button, open the modal 
    btn.onclick = function() {
        modal.style.display = "block";
    }

    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
        modal.style.display = "none";
    }

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
</script>

Desire Output for all members

对于冗长的帖子感到抱歉,但只想说清楚。 任何帮助或更正都会非常棒。 谢谢

【问题讨论】:

  • document.getElementById('myModal1','myModal2','myModal3') ?你不能这样做..
  • 您的代码无需 javascript 就可以正常工作...只有两个小问题 - 删除 .modal CSS 中的 z-index,并将 data-dismiss="modal" 属性添加到您的 close纽扣。就是这样,不需要脚本。检查JSFiddle demo
  • 感谢两位的快速回复。 @ARTUR-FILIPIAK 是否有可能其他一些与模态 bcoz JSFiddle 混淆的工作,但我页面中的相同代码甚至没有创建新页面,只有 JSFiddle 代码仍然无法正常工作。
  • 控制台有错误吗?
  • 没有错误,似乎按钮在没有脚本的情况下不起作用,但使用document.getElementById('myModal1'),脚本只有第一个模态工作。有什么办法可以将此行与通用的每个模态一起使用?

标签: jquery html css twitter-bootstrap bootstrap-modal


【解决方案1】:

好的,我只需在标题中添加 bootstrap.min.js 脚本链接就可以了。 感谢所有这一切现在是一个完整的工作代码。 如果像我这样的人需要,我会编辑上面的代码以供将来参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多