简体   繁体   English

对于一个元素中的每一个,迭代其他元素的列表。 元素没有同级/父级关系

[英]For each of one element, iterate over a list of other elements. Elements have no sibling/parent relationship

Example code: 示例代码:

codepen 码笔

<div>
  <button class="mainButton">CLICK ME!</button>
</div>
<div>
  <div class="subElement">David</button>
  <div class="subElement">Joe</button>
</div>

<div>
  <button class="mainButton">AND ME!</button>
</div>
<div>
  <div class="subElement">Billy</button>
  <div class="subElement">Bob</button>
</div>

For each mainButton, I want to iterate over the subElements it corresponds to by either finding the ones below it, or maybe adding some sort of reference? 对于每个mainButton,我想通过找到其下方的子元素来进行迭代,或者添加某种引用?

Can anyone provide any solutions to this issue? 谁能为这个问题提供任何解决方案?

You'll need to group the subElements with separate class names then use jQuery to get all their values. 您需要使用单独的类名称对子元素进行分组,然后使用jQuery获取其所有值。

<div>
  <button class="mainButton" onclick="main1()">CLICK ME!</button>
</div>
<div>
  <div class="subElement1">David</button>
  <div class="subElement1">Joe</button>
</div>

<div>
  <button class="mainButton" onclick="main2()">AND ME!</button>
</div>
<div>
  <div class="subElement2">Billy</button>
  <div class="subElement2">Bob</button>
</div>

I would recommend better class names, but this is just for example. 我会建议使用更好的类名,但这仅是示例。 Then you can grab their values in your js: 然后,您可以在js中获取它们的值:

function main1() {
  var sub1s = $('.subElement1').val();  //this will be an array of their values
}

function main2() {
  var sub2s = $('.subElement2').val();  //also an array
}

If you cannot change anything to your HTML, then I think this would be a way to do it: 如果您无法更改HTML的任何内容,那么我认为这是一种实现方法:

 $('.mainButton').click(function() { var texts = []; var $all = $('.mainButton,.subElement'); $all.slice($all.index(this)+1).each(function() { if ($(this).is('.mainButton')) return false; // break out texts.push($(this).text()); }); console.log(texts); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <button class="mainButton">CLICK ME!</button> </div> <div> <div class="subElement">David</div> <div class="subElement">Joe</div> </div> <span> <div> <button class="mainButton">AND ME!</button> </div> <div> <div class="subElement">Billy</div> <div class="subElement">Bob</div> </div> 

This collects all buttons and sub elements in one collection. 这将收集一个集合中的所有按钮和子元素。 Then it finds the index where the currently clicked button is positioned in that collection. 然后,它找到该集合中当前单击的按钮所处的索引。 All elements that come before this index, and also the clicked button itself, are sliced out of the collection. 此索引之前的所有元素,以及单击的按钮本身,都从集合中切出。 Now all following sub elements are those we are interested in, up until the end of the collection or the next main button. 现在,以下所有子元素都是我们感兴趣的子元素,直到集合结束或下一个主按钮为止。

For this solution it does not matter how deep things are nested, as long as their sequence in the DOM tree is such that all sub elements follow the main button to which they "belong". 对于此解决方案,嵌套的对象有多深无关紧要,只要它们在DOM树中的顺序是所有子元素都遵循它们“所属”的主按钮即可。

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

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