简体   繁体   English

无法遍历数组,避免添加ID

[英]Trouble looping through array, avoid adding ids

Looking to: 寻找:

Have the data for each of the 56 people show up in a popup window/ "tooltip" when you click on the image of their face (see index.html ) without having to use ids. 单击他们的脸部图像(请参阅index.html )时,无需使用ID,即可在弹出窗口/“工具提示”中显示56个人的每个人的数据。 For example, clicking on Allan's image should give you her data stored in var MLA tried using a for loop to little success, maybe .each() 例如,单击Allan的图像应为您提供她存储在var MLA数据,尝试使用for循环获得很少的成功,也许是.each()

There are two MLAs here in the example, scripts.js , but there are actually a total of 56 items that I'm trying to iterate over. 在示例中,这里有两个MLA,即scripts.js ,但实际上我要迭代的共有56个项目。 Right, now I'm getting the last person in the array. 对,现在我要找到数组中的最后一个人。

scripts.js (Data, attempted for loop) scripts.js(数据,尝试循环)

    // MLAs
         var MLAs = [
           {
             "Name": "Nancy Allan",
             "Age": 62,
             "Constuency": "St. Vital",
             "Party": "NDP",
             "Gender": "Female",
             "Ethnicity": "White"
           },
           {
             "Name": "James Allum",
             "Age": null,
             "Constuency": "Fort Garry-Riverview",
             "Party": "NDP",
             "Gender": "Male",
             "Ethnicity": "White"
           }]

     // Shows a popup with MLA information
     $(".headshot").click(function(){
          $(".tooltip").css("display", "block");
          for (i = 0; i < 56; i++) {
              $(".tooltipName").html(MLAs[i].Name);
              $(".tooltipParty").html(MLAs[i].Party);
              $(".tooltipConstuency").html(MLAs[i].Constuency);
              $(".tooltipEthnicity").html(MLAs[i].Ethnicity) + ",";
              $(".tooltipAge").html(MLAs[i].Age);
          }
     });
});

Using #ids 使用#id

Doesn't really solve the problem, is there a better solution? 真的不能解决问题,有更好的解决方案吗?

index.html index.html

<img src="assets/img/headshots/allan.jpg" id="0" alt="" class="headshot NDP Female White">
<img src="assets/img/headshots/allum.jpg" id="1" alt="" class="headshot NDP Male White">
<img src="assets/img/headshots/altemeyer.jpg" id="2" alt="" class="headshot NDP Male White">

scripts.js scripts.js

$(".headshot").click(function(){
        index = this.id;

        $(".tooltip").css("display", "block");
            $(".tooltipName").html(MLAs[index].Name);
            $(".tooltipParty").html(MLAs[index].Party);
            $(".tooltipConstuency").html(MLAs[index].Constuency);
            $(".tooltipEthnicity").html(MLAs[index].Ethnicity); + ","
            $(".tooltipAge").html(MLAs[index].Age);
    });
});

You shouldn't use numeric id attribute values. 您不应该使用数字id属性值。 You could simply use their .index() within a selector. 您可以在选择器中简单地使用它们的.index()。

<div id="people">
<img src="assets/img/headshots/allan.jpg" alt="" class="headshot NDP Female White">
<img src="assets/img/headshots/allum.jpg" alt="" class="headshot NDP Male White">
...
</div>

- --

$(".headshot").on('click', function() {
    var idx = $(this).index();
    $(".tooltip").css("display", "block");
        $(".tooltipName").html(MLAs[idx].Name);
        $(".tooltipParty").html(MLAs[idx].Party);
        $(".tooltipConstuency").html(MLAs[idx].Constuency);
        $(".tooltipEthnicity").html(MLAs[idx].Ethnicity); + ","
        $(".tooltipAge").html(MLAs[idx].Age);
    });
});

You can use the data attribute as described in the following article. 您可以按照以下文章中所述使用data属性。

I have provided the code snippet to review. 我提供了代码段进行审查。 Please notice that I added key to the JSON data to uniquely identify the record. 请注意,我将密钥添加到JSON数据中以唯一地标识记录。 If you can not do it, you can create a map[key:JsonObject] instead. 如果您不能执行此操作,则可以创建一个map [key:JsonObject]。

 var MLAs = [{ "Key": "allan", "Name": "Nancy Allan", "Age": 62, "Constuency": "St. Vital", "Party": "NDP", "Gender": "Female", "Ethnicity": "White" }, { "Key": "allum", "Name": "James Allum", "Age": null, "Constuency": "Fort Garry-Riverview", "Party": "NDP", "Gender": "Male", "Ethnicity": "White" }]; function findObj(key) { var result = null; $.each(MLAs, function(i, obj) { if (obj.Key == key) result = obj; }); return result; } $(document).ready(function() { $(".headshot").click(function() { var key = $(this).data('key'); var obj = findObj(key); if (obj != null) { $(".tooltip").css("display", "block"); $(".tooltipName").html(obj.Name); $(".tooltipParty").html(obj.Party); $(".tooltipConstuency").html(obj.Constuency); $(".tooltipEthnicity").html(obj.Ethnicity); + "," $(".tooltipAge").html(obj.Age); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="https://lh3.googleusercontent.com/-j10EGNjCpSg/AAAAAAAAAAI/AAAAAAAAAE0/QQmvymOupfE/photo.jpg?sz=32" data-key="allan" class="headshot NDP Female White"> <img src="https://www.gravatar.com/avatar/368596193089a24f609307b3af288ae6?s=32&d=identicon&r=PG" data-key="allum" class="headshot NDP Male White"> <table class="tooltip"> <tr> <td class="tooltipName"></td> <td class="tooltipParty"></td> <td class="tooltipConstuency"></td> <td class="tooltipEthnicity"></td> <td class="tooltipAge"></td> </tr> </table> 

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

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