繁体   English   中英

每页有多个模态

[英]Having Multiple Modals per page

我正在创建一个网站,用户可以在其中单击图像以打开包含菜单项的模态,而第一个模态可以正常工作,而后续模态则不能。

JS代码:

<script>
// Get the modal
var modal = document.getElementById('myModal');

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

// 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>

使用HTML的地方:

    <div class="col-md-3">
        <h2>Starters</h2>
        <img src="http://foodnetwork.sndimg.com/content/dam/images/food/fullset/2007/2/9/0/ie0102_coleslaw.jpg" height="100%" width="100%" id="myImg">
                <!-- The Modal -->
                <div id="myModal" class="modal">

                  <!-- Modal content -->
                  <div class="modal-content">
                    <div class="modal-header">
                      <span class="close">×</span>
                      <br>
                      <h2>Starters</h2>
                    </div>
                    <div class="modal-body">
                      <p><b>PRODUCT <span style="display:inline-block; width: 75%;"></span> PRICE</b></p>
                      <hr>
                      <p> Humus <span style="display:inline-block; width: 78%;"></span>  £3.00</p>
                      <p> Dolma <span style="display:inline-block; width: 78%;"></span>  £3.20</p>
                      <p> Coleslaw <span style="display:inline-block; width: 77%;"></span>  £1.50</p>
                      <p> Prawn Cocktail <span style="display:inline-block; width: 73.5%;"></span>  £4.60</p>
                    </div>

                  </div>

                </div>

    </div>

    <div class="col-md-3">
        <h2>Wraps</h2>
        <img src="http://harryskebabs.com/images/demo/menu-main/menu-item-large-20.jpg" height="100%" width="100%" id="myImg">

                <div id="myModal" class="modal">

                  <!-- Modal content -->
                  <div class="modal-content">
                    <div class="modal-header">
                      <span class="close">×</span>
                      <br>
                      <h2>Starters</h2>
                    </div>
                    <div class="modal-body">
                      <p><b>PRODUCT <span style="display:inline-block; width: 75%;"></span> PRICE</b></p>
                      <hr>
                      <p> Chicken Wrap <span style="display:inline-block; width: 78%;"></span>  £3.00</p>
                      <p> Doner Wrap <span style="display:inline-block; width: 78%;"></span>  £3.20</p>
                      <p> Shish Wrap <span style="display:inline-block; width: 77%;"></span>  £1.50</p>
                      <p> Kofte Wrap <span style="display:inline-block; width: 73.5%;"></span>  £4.60</p>
                    </div>

                  </div>

                </div>

    </div>

显示的第一个模式框将按预期工作,而第二个模式框将完全不显示,如果不需要,我宁愿不必不必要地复制JavaScript,这就是为什么我厌倦了这种方法的原因。

JS和模式框非常新,因此,感谢任何输入。

modal.style.display = nonemodal是指ID为“ Modal”的元素。 您有两个具有此元素的元素, 但是 HTML元素的id需要唯一

这适用于所有id ,因此您还必须更改图片的唯一ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM