将数据传递给jQuery模式

时间:2016-09-22 18:19:35

标签: javascript jquery

使用从头开始构建的简单模态。没有任何花哨的添加。但是我需要从数组中传递数据,到目前为止,我唯一能得到的是添加到所有可点击元素的数组的最后一个成员。请告知如何确保在点击时传递适当的数据。

 <div id="skills-mobile">
     <!-- popup content -->
     <div id="myModal" class="modal">
         <img class="close-icon" src="img/close-icon.png" alt="cross or close the window icon">
         <article class="modal-content">

         </article>
     </div>
 </div>

这是正在使用的对象数组:

var skills = [
        {
            "id":"html-5",
            "skill":"HTML5",
        },
        {
            "id":"css-3",
            "skill":"CSS3"
        },
        {
            "id":"jvscrpt",
            "skill":"Javascript"
        },
        {
            "id":"inter-map",
            "skill":"Interaction Mapping"
        },
        {
            "id":"wire",
            "skill":"Wireframing"
        },
        {
            "id":"docum",
            "skill":"Documentation"
        },
        {
            "id":"adobecc",
            "skill":"Adobe CC"
        }
    ]

这是jQuery。现在,当我关闭模态并再次打开它时,它也会一遍又一遍地输出值。因此,它基本上会一遍又一遍地附加它,我需要解决它,我知道。如果可能的话,请提供我做错的指导。我认为结构是不对的。

var skillSlideshow = function() {

        for(var i = 0; i <= skills.length; i++) {

            $('#skills-mobile').append('<img id="' + skills[i].id + '" class="skill-img" data-toggle="modal" data-id="107" src="' + skills[i].iconurl + '" />');

            //MODALS
            // Get the modal
            var modal = document.getElementById('myModal');

            // Get the button that opens the modal
            var imgbtn = document.getElementById(skills[i].id);

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

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

            // When the user clicks on <span> (x), close the modal
            closebtn.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";
                }
            }
        }
    };
    skillSlideshow();

1 个答案:

答案 0 :(得分:1)

这应该有效......

&#13;
&#13;
var skills = [
        {
            "id":"html-5",
            "skill":"HTML5",
        },
        {
            "id":"css-3",
            "skill":"CSS3"
        },
        {
            "id":"jvscrpt",
            "skill":"Javascript"
        },
        {
            "id":"inter-map",
            "skill":"Interaction Mapping"
        },
        {
            "id":"wire",
            "skill":"Wireframing"
        },
        {
            "id":"docum",
            "skill":"Documentation"
        },
        {
            "id":"adobecc",
            "skill":"Adobe CC"
        }
    ]
    var clearModal = function() {
      $('#myModel').css('display', 'none')
      $(".modal-content").empty();
    }
    var skillSlideshow = function() {

        for(var i = 0; i < skills.length; i++) {
            $('#skills-mobile').append('<img id="' + skills[i].id + '" class="skill-img" data-toggle="modal" data-id="107" data-index="'+ i +'"src="' + skills[i].iconurl + '" />')
        }
      
        $('.skill-img').on('click', function(e) {
          $('#myModel').css('display', 'block');
          var data = skills[parseInt(e.currentTarget.dataset.index)]
          //injecting the data into the modal
          const html = "<div>id:" + data.id + "</div><div>skill:" + data.skill + "</div>"
          $('.modal-content').html(html)
        })
        
        $('.close-icon').on('click', clearModal)
        $('#myModal').on('click', function(e) {
          //assuming you give proper styling to .modal-content and .modal
          if(e.target.id === 'myModal') clearModal()
        })
    };
    skillSlideshow();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="skills-mobile">
     <!-- popup content -->
     <div id="myModal" class="modal">
         <img class="close-icon" src="img/close-icon.png" alt="cross or close the window icon">
         <article class="modal-content">

         </article>
     </div>
 </div>
&#13;
&#13;
&#13;