简体   繁体   English

如何将每个div包装到多个元素

[英]How to wrap each divs to multiple elements

This is how it is: 这是这样的:

<button> One <button> 
<button> Two <button> 
<button> Three <button>     

what I'm trying to do is like this: 我想做的是这样的:

<div><button> One <button></div> 
<div><button> Two <button></div>   
<div><button> Three <button></div>   

Javascript: 使用Javascript:

var theDivs = document.createElement("div");

var divWrapper = new Array(3).fill(theDivs);

for (var i = 0; i < divWrapper.length; i++) {

  divWrapper[0].appendChild(buttonOne);
  divWrapper[1].appendChild(buttonTwo);
  divWrapper[2].appendChild(buttonThree);

  document.body.appendChild(divWrapper[0]);
  document.body.appendChild(divWrapper[1]);
  document.body.appendChild(divWrapper[2]);

};

You need to create separate <div> elements, one on each loop iteration, or else all of the array elements will be the same <div> . 您需要创建单独的<div>元素,每个循环迭代一个,否则所有数组元素都将是相同的<div>

Here's what you're doing: 这是您正在做的事情:

var divWrapper = new Array(3).fill(theDivs);

This means all three elements of the divWrapper array are the reference to the same <div> object, which you don't want. 这意味着divWrapper数组的所有三个元素都是对同一个<div>对象的引用,而您不需要。

Also, you can create DRY-er code by only creating and appending elements once, inside the loop, instead of copying it three times like you do here: 另外,您可以通过在循环内仅创建和附加一次元素来创建DRY-er代码,而不是像在这里所做的那样将其复制三遍:

divWrapper[0].appendChild(buttonOne);
divWrapper[1].appendChild(buttonTwo);
divWrapper[2].appendChild(buttonThree);

document.body.appendChild(divWrapper[0]);
document.body.appendChild(divWrapper[1]);
document.body.appendChild(divWrapper[2]);

Demo: 演示:

 for (var i = 0; i < 3; i++) { var divWrapper = document.createElement("div"); var button = document.createElement("button"); button.textContent = i + 1; divWrapper.appendChild(button); document.body.appendChild(divWrapper); }; 
 div { /* make <div>s clearer */ background-color: grey; margin: 1em; } 


As per your comment, below is a demo to show you what will happen if you do it your way, filling an array with one element. 根据您的评论,下面是一个演示,向您展示如果您按自己的方式操作,将用一个元素填充数组的情况。 Although you are accessing different elements of the array with divWrappers[i] in the loop, they all refer to the same object, so that all the buttons are added to the same <div> . 尽管您在循环中使用divWrappers[i]访问数组的不同元素,但它们都引用同一对象,因此所有按钮都添加到同一<div>

 var divWrapper = document.createElement("div"); var divWrappers = Array(3).fill(divWrapper); for (var i = 0; i < divWrappers.length; i++) { var button = document.createElement("button"); button.textContent = i + 1; divWrappers[i].appendChild(button); document.body.appendChild(divWrappers[i]); }; 
 div { background-color: grey; margin: 1em; } 

Try using jQuery's .wrapAll() 尝试使用jQuery的.wrapAll()

Include jQuery in your HTML (see the website above if you need further information on how to do that). 在您的HTML中包含jQuery(如果需要有关如何执行此操作的更多信息,请参见上面的网站)。

Then use: 然后使用:

$(":button").wrapAll("<div class=''/>");

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

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