单击.div中的div附加循环

时间:2018-08-17 10:23:35

标签: javascript jquery

我正在使用.append填充div-id,并且一切正常,我什至在其中添加了循环,但是我想使一个项目在循环内可点击,并加载一个保存该项目详细信息的div 。这就是我到目前为止所得到的。

<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>');

      }
 })

//基于K K的解[以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)

    });

因此,我找到了通过使用数组中的find将两种数据组合在一起的不同方法。有更好的方法吗?如果是这样,为什么?有什么区别?

2 个答案:

答案 0 :(得分:1)

尝试类似的操作

  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
  });

注意:这里的方法是将ajax响应存储在变量中。从您的代码中,响应是一个数组。因此,当您遍历各个项目时,可以按照自己喜欢的任何方式获取被单击项目的索引,并使用来自gamesData的索引访问游戏的详细信息。

答案 1 :(得分:0)

您可以将data-id添加到动态广告中,例如:<div data-id="'+data[i].id+'"
那么你可以做:

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
});