简体   繁体   English

单击.div中的div附加循环

[英]click a div in an .append with a loop

I am using an .append to populate a div-id and all that works fine, i even get the loop inside, however i would like to make an item clickable inside the loop and load a div that holds details of that item. 我正在使用.append填充div-id,并且一切正常,我什至在其中添加了循环,但是我想使项目在循环内可点击并加载包含该项目详细信息的div。 This is what i got so far. 这就是我到目前为止所得到的。

<div id="GameContainer"></div>

var gamesData; //A global variable to hold Ajax response.

$.ajax({
     type: 'Get',
     url: "http://"URL/" + GamesList,
     success: function (data) {

       gamesData = data; // add the Ajax data to the global variable

       var dynamic = "";

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

       dynamic += '<div id="' + data[i].id_game + '" class="TopContainerCel" onclick="GameDetails(' + data[i] + ')">'
               + '    <div class="TopContainerCelBackground">'
               + '          <img class="TopContainerCelImage" src="' + data[i].CoverImage + '" />'
               + '       </div>'
               + '    <div class="TopContainerCelName">' + data[i].Name + '</div>'
               + ' </div>'
             };

        $('#GameContainer').append(''
             + '<div class="TopContainerScroll">'
             + dynamic
             + '</div>');

      }
 })

// based on the solution of KK [extended with an array.find] //基于KK的解决方案[以array.find扩展]

    added the global variable gamesData and filled it with the Ajax reponse

    $(document).on("click", ".TopContainerCel", function () {
        var elem = $(this);

        console.log(elem[0].id) // the actual id clicked is there
        console.log(gamesData) // all the data of the Ajax response is there

        GameID = elem[0].id;

        var gameData = gamesData[elem.data("id")]; // part that does not work
        var gameData = gamesData.find(x => x.id_game == GameID); // works!

       //gameData has the data
        console.log(gameData)

    });

So i found a diffent way of combining the two data together by using a find in the array. 因此,我找到了一种通过使用数组中的查找将两种数据组合在一起的不同方法。 Is there a better way of doing this? 有更好的方法吗? If so why and what is the difference? 如果是这样,为什么和有什么区别?

Try something similar to this: 试试类似的东西:

  var gamesData;//A global variable to hold Ajax response.
  $.ajax({
       type: 'Get',
       url: "http://URL/" + GamesList,
       success: function (data) {
       gamesData = data;

         var dynamic = "";

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

         dynamic += '<div id="' + data[i].id_game + '" data-id="'+data[id]+'" class="TopContainerCel">'
                 + '    <div class="TopContainerCelBackground">'
                 + '          <img class="TopContainerCelImage" src="' + data[i].CoverImage + '" />'
                 + '       </div>'
                 + '    <div class="TopContainerCelName">' + data[i].Name + '</div>'
                 + ' </div>'
               };

          $('#GameContainer').append(''
               + '<div class="TopContainerScroll">'
               + dynamic
               + '</div>');

        }
   })
  $(document).on("click",".TopContainerCel",function(){
    var elem = $(this);
    var gameData = gamesData[elem.data("id")];
    //gameData has your data
  });

Note: The approach here is to store ajax response in a variable. 注意:这里的方法是将ajax响应存储在变量中。 From your code, the response is an array. 从您的代码中,响应是一个数组。 So, when you iterate over the items, get the index of the clicked item in any way you prefer and access the detail of game using the index from gamesData. 因此,当您遍历项目时,可以按自己喜欢的任何方式获取被单击项目的索引,并使用来自gamesData的索引访问游戏的详细信息。

you can add data-id to dynamic like : <div data-id="'+data[i].id+'" . 您可以将data-id添加到动态对象中,例如: <div data-id="'+data[i].id+'"
then you can do : 那么你可以做:

var games = {};

for (i = 0; i < data.length; i++) {
       games[data[i].id_game] = data[i];
       dynamic += '<div id="' + data[i].id_game + '" class="TopContainerCel" onclick="GameDetails(' + data[i] + ')">'
               + '    <div class="TopContainerCelBackground">'
               + '          <img class="TopContainerCelImage" src="' + data[i].CoverImage + '" />'
               + '       </div>'
               + '    <div class="TopContainerCelName">' + data[i].Name + '</div>'
               + ' </div>'
};

$("#GameContainer").on('click','.TopContainerCel',function() {
  var $this = $(this);

  console.log(games[$this.data('id')])
  // code logic here
});

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

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