简体   繁体   English

如何使jQuery函数遍历从同一类的2个列表获取的单独数组中?

[英]How can I make a jQuery function iterate over separate arrays taken from 2 lists in the same class?

Very sorry if the title is confusing, I'm very new to Javascript/jQuery and I don't really have the terminology down yet. 非常抱歉,如果标题令人困惑,我是Javascript / jQuery的新手,并且我还没有真正的术语。 I'm in charge of a gaming group and trying to keep track of how many points each member racks up. 我负责一个游戏小组,并试图跟踪每个成员的得分。 For simplicity, I'm just using two lists for now, one for Suzy's points and one for Billy's points. 为了简单起见,我现在仅使用两个列表,一个用于Suzy的积分,另一个用于Billy的积分。

I currently have a function that adds up the points (it takes any bolded text that's contained in an HTML list item and sums the values) and returns the total sum in a designated div under each list. 我目前有一个将这些点加起来的函数(它接受HTML列表项中包含的任何加粗文本并对其值求和),并在每个列表下的指定div中返回总和。 The problem is that it's returning the sum of ALL the points (in both lists), instead of the points for that individual person. 问题在于,它返回的是所有点(在两个列表中)的总和,而不是该个人的点数。

 $(".person").each(function() { var arr = []; $("li b").each(function() { arr.push($(this).text()); }) var total = 0; for (var i = 0; i < arr.length; i++) { total += arr[i] << 0; } $(".total").text("Total: " + total); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="member"> <div class="points"> <ul class="person" id="suzy"> <h1>Suzy</h1> <li><b>40</b> points</li> <li><b>50</b> points</li> </ul> </div> <div class="total" style="background:orange"></div> </div> <div class="member"> <div class="points"> <ul class="person" id="billy"> <h1>Billy</h1> <li><b>10</b> points</li> <li><b>20</b> points</li> </ul> </div> <div class="total" style="background:orange"></div> </div> 

I understand what's going wrong with the code, I just don't know enough about jQuery to figure out what I need to replace it with. 我了解代码出了什么问题,我只是对jQuery不够了解,无法弄清楚我需要用什么替换它。 Basically, how do I make it so that the array created from $("li b") in the third line is limited to the instances of "li b" that occur within the particular ".person" element specified in the first line ? 基本上,如何使第三行中由$(“ li b”)创建的数组限于出现在第一行中指定的特定“ .person”元素内的 “ li b”实例?

You need to use $(this) to refer to the .person you're looping over. 您需要使用$(this)来引用要遍历的.person So for example, instead of $("li b").each use $(this).find("li b").each . 因此,例如,代替$("li b").each使用$(this).find("li b").each Same logic for the placement of the total calculation: 放置total计算的逻辑相同:

 $(".person").each(function() { var arr = []; $(this).find("li b").each(function() { arr.push($(this).text()); }) var total = 0; for (var i = 0; i < arr.length; i++) { total += arr[i] << 0; } $(this).closest('.member').find(".total").text("Total: " + total); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="member"> <div class="points"> <ul class="person" id="suzy"> <h1>Suzy</h1> <li><b>40</b> points</li> <li><b>50</b> points</li> </ul> </div> <div class="total" style="background:orange"></div> </div> <div class="member"> <div class="points"> <ul class="person" id="billy"> <h1>Billy</h1> <li><b>10</b> points</li> <li><b>20</b> points</li> </ul> </div> <div class="total" style="background:orange"></div> </div> 

Your getting the same number in both spots because $(".total") has 2 matches. 因为$(".total")有2个匹配项,所以在两个位置上得到的数字相同。

Also, there is no need for an array here. 另外,这里不需要数组。 It just makes the code more complicated and it doesn't make any sense to store a second copy of the data - now you have to make sure that the array data stays in sync with the HTML data. 这只会使代码变得更加复杂,而存储数据的第二个副本则没有任何意义-现在您必须确保数组数据与HTML数据保持同步。 Instead, just get the data from the HTML. 相反,只需从HTML获取数据即可。

Now, you've got some HTML validation issues: 现在,您遇到了一些HTML验证问题:

  • An h1 isn't allowed inside of a ul . 不允许在ul内部使用h1
  • The b element shouldn't be used solely for styling. b元素不应仅用于样式。

So, you're going to have to adjust your HTML. 因此,您将不得不调整HTML。 And, if you swap the closing points div and the total div , the solution gets a little easier. 而且,如果交换关闭points divtotal div ,则解决方案会更容易一些。 See the comments below for details: 有关详细信息,请参见以下评论:

 $(".person").each(function(){ var total = 0; // Loop over only the points for the current person, not all the points // The second argument passed to JQuery provides context for where to // limit the first argument query. "this" refers to the current person // that the loop is iterating over, so you only are adding up one person's // points, not both. $("span.points", this).each(function(){ // No JQuery needed here. Just convert the text of "this", which now (because // we are in a different loop) points to the span that we're looping over, // to a number and add it to the total total += +this.textContent; }); // No JQuery needed here either. Just set the text of the next sibling of // the person we were looping over (now the total div) to the total. this.nextElementSibling.textContent = "Total: " + total; }); 
 /* Do styling with CSS classes rather than inline styles */ li span.points { font-weight:bold; } .total {background:orange; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="member"> <div class="points"> <h1>Suzy</h1> <ul class="person"> <li><span class="points">40</span> points</li> <li><span class="points">50</span> points</li> </ul> <div class="total"></div> </div> </div> <div class="member"> <div class="points"> <h1>Billy</h1> <ul class="person"> <li><span class="points">10</span> points</li> <li><span class="points">20</span> points</li> </ul> <div class="total"></div> </div> </div> 

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

相关问题 如何迭代对象中的对象和对象数组,并在每个对象中运行相同的函数? - How can I iterate over objects and arrays of objects in an object and run the same function in each object? 如何使用相同的函数迭代对象/数组(不使用forEach) - How to iterate over objects/arrays using the same function (without forEach) 如何在同一个函数中进行 100 次调用,以遍历 api 端点? [口袋妖怪] - How do I make 100 calls in the same function, to iterate over api endpoints? [Pokeapi] 如何迭代多个数组 - Jquery - How to iterate over multiple arrays - Jquery Selenium IDE:如何同时迭代 2 个列表? 我无法对集合进行迭代 - Selenium IDE: How do I iterate simultaneously over 2 lists? I can't get the iterate over a collection to work 如何使用jquery(或只是javascript)迭代cookie? - How can I iterate over cookies using jquery (or just javascript)? 如何使这些数组正常运行? - How can I make these arrays function properly? 如何同时映射两个数组? - How can I map over two arrays at the same time? 如何从 Firestore 检索 map 并对其进行迭代? - How can I retrieve a map from Firestore and iterate over it? 给定两个数组,如何遍历一个数组并推入第二个数组的重复元素? - Given two arrays, how can I iterate over one and push to repeated elements of the second?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM