简体   繁体   English

将具有相同类的多个元素分发给具有相同类的其他元素

[英]Distribute multiple elements with same class to other elements with same class

Hello need some help to distribute multiple elements with same class to other multiple elements with same class. 你需要一些帮助来将具有相同类的多个元素分发给具有相同类的其他多个元素。 I think maybe .appendTo can help with this. 我想也许.append可以帮助解决这个问题。

I want to move these: ... 我想移动这些:...

<div class="button">
...
</div>

<div class="button">
...
</div>

<div class="button">
...
</div>

into these: ... 进入这些:......

<div class="button_place">
...
</div>

<div class="button_place">
...
</div>

<div class="button_place">
...
</div>

so that I have these: first button on first button_place and so on... 所以我有这些:第一个button_place上的第一个按钮,依此类推......

<div class="button_place">
  <div class="button">
    ...
  </div>
</div>

<div class="button_place">
  <div class="button">
    ...
  </div>
</div>

<div class="button_place">
  <div class="button">
    ...
  </div>
</div>

Select all buttons and places. 选择所有按钮和位置。 Then iterate over buttons and append them to corresponding places container. 然后迭代按钮并将它们附加到相应的位置容器。

jQuery version: jQuery版本:

const $places = $('.button_place')

$('.button').each(function(i) {
  $places[i].appendChild(this)
})

Pure JS version: 纯JS版:

const buttons = document.querySelectorAll('.button')
const places  = document.querySelectorAll('.button_place')

buttons.forEach(function(button, index) {
  places[index].appendChild(button)
})
// NodeList.prototype.forEach is not supported in IE,
// convert NodeList to array first with slice, Array.from, etc.

Simply use with each() of $('.button_place') .and append($('.button').eq(a).clone()).html() its used to get the outerhtml of button 只需使用$('.button_place') each()$('.button_place') append($('.button').eq(a).clone()).html()用于获取button的outerhtml

 $('.button_place').each(function(a, b) { $(this).append($('.button').eq(a).clone()).html() }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="button"> button</div> <div class="button"> button</div> <div class="button"> button</div> <div class="button_place">button_place</div> <div class="button_place">button_place</div> <div class="button_place">button_place</div> 

Select both sets, loop over one set, and append to the other set. 选择两个集合,循环遍历一个集合,然后附加到另一个集合。

 var buttons = $(".button"), places = $(".button_place"); buttons.each(function(i, elem) { places.eq(i).append(elem) }) 
 .button_place { border: 1px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="button"> b1 </div> <div class="button"> b2 </div> <div class="button"> b3 </div> <div class="button_place"> </div> <div class="button_place"> </div> <div class="button_place"> </div> 

You can do it using each function as: 你可以使用每个函数来做到:

https://jsfiddle.net/o2gxgz9r/6623/ https://jsfiddle.net/o2gxgz9r/6623/

$(".button_place").each(function(index,elem){
 //$(elem).html($(".button").eq(0));
 $(elem).append($(".button").eq(0));
})

use html function if you want to replace content and use append if you want to add content at the end. 如果要替换内容,请使用html函数;如果要在末尾添加内容,请使用append。

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

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