我怎样才能得到多个模态?

时间:2017-04-11 16:31:47

标签: javascript html

我正在使用循环从YML文件生成元素,并且对于我希望能够单击它们的每个元素,并在YML文件中概述了不同的内容。但是,当点击它工作的第一个元素时,但是当点击页面中的任何其他元素时,模态甚至不再出现。

{% for company in site.data.stories %}
    <div class="col-md-6 col-sm-6  col-xs-12 company" style="height: 600px;">
        <div class="img-container">

            <!-- Trigger/Open The Modal -->
            <button id="myBtn">
            <img class="center" style="width: 486px; height: 364.5px;" id="{{company.image_url}}" src="/assets/img/stories/{{company.image_url}}.png" alt="{{company.name}}"></button>

            <!-- The Modal -->
            <div id="myModal" class="modal">
            <!-- Modal content -->
            <div class="modal-content">
            <div class="modal-header">
                <span class="close">&times;</span>
                <h2>{{company.name}}</h2>
            </div>
            <div class="modal-body">
                <strong>Date of Event: </strong>{{company.date_of_event}}
                <p>{{company.fullstory}}</p>
            </div>
            <div class="modal-footer">
            <h3>The End</h3>
            </div>
            </div>
            </div>
        </div>

            <h3><a href="{{company.site_url}}" class="center">{{company.name}}</a></h3> 
            <p><b>Date of Event: </b> {{company.date_of_event}}</p>
            <p>{{company.description}}</p>
    </div>
    {% endfor %}

Java脚本:

<script> // Modal Script
// Get the modal
var modal = document.getElementById('myModal');
// 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>

YML文件中的每个元素都如下所示:

name: The Delgado Household Story
  description: The Joy Project helps a single mother of eight with time and donations.
  image_url: delgado_photo2
  site_url: #
  date_of_event: Coming Soon...
  width: 90
  margin: 5
  fullstory: 

1 个答案:

答案 0 :(得分:1)

您必须将每个模态ID重命名为不同的内容,现在它们都是使用ID myModal动态生成的。触发每个模态的按钮也必须知道要触发的模态。您可以将模态的ID设置为特定于每组数据的ID,例如name或为每条记录添加唯一ID属性。

要保存加载一堆模态...而不是根据您的数据动态创建模态(重用ID myModal),尝试重用单个模态可能是一个好主意,根据单击的内容动态地将内容添加到该模式。您仍然可以逐个加载所有公司数据(将模式保留在html中的for循环中),然后单击模态按钮时,将其余数据注入要显示在模态中。

{% for company in site.data.stories %}
<div class="col-md-6 col-sm-6  col-xs-12 company" style="height: 600px;">
    <div class="img-container">

        <!-- Trigger/Open The Modal -->
        <button id="{{company.uniqueID}}">
        <img class="center" style="width: 486px; height: 364.5px;" id="{{company.image_url}}" src="/assets/img/stories/{{company.image_url}}.png" alt="{{company.name}}"></button>
    </div>

        <h3><a href="{{company.site_url}}" class="center">{{company.name}}</a></h3> 
        <p><b>Date of Event: </b> {{company.date_of_event}}</p>
        <p>{{company.description}}</p>
</div>
{% endfor %}


<!-- The Modal -->
<div id="myModal" class="modal">
    <!-- Modal content -->
    <div class="modal-content">
        <div class="modal-header">
            <span class="close">&times;</span>
            <h2>{{company.name}}</h2>
        </div>
        <div class="modal-body">
            <strong>Date of Event: </strong>{{company.date_of_event}}
            <p>{{company.fullstory}}</p>
        </div>
        <div class="modal-footer">
            <h3>The End</h3>
        </div>
    </div>
</div>

注意:您可能必须考虑如何以不同方式构建数据。也许给每个按钮一个唯一的ID并获取与该唯一ID <button id="{{company.uniqueID}}">相关的数据。

您还可以根据名称“The Delgado Household Story”等设置ID提取... <button id="{{company.name}}">,然后根据按钮ID注入,例如get button ID name, find and match name in dataset, then set modal data based off of properties in that 'name'