简体   繁体   English

使用javascript将n个div包装在一个父对象中

[英]wrap n number of div in one parent using javascript

I have list of html element which has been generated by javascript. 我有由javascript生成的html元素列表。 Now Every 4 (could be different) should have one parent. 现在,每4个(可能不同)应该有一个父级。 The problem is how do we close the div after 4th element. 问题是我们如何在第4个元素之后关闭div。

Here's a snippet (or here's a fiddle ): 这是一个片段(或这是一个小提琴 ):

 var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] var html = ''; for (var i = 0; i < x.length; i++) { if (i % 3 === 0) { html += '<div class="parent">'; } html += '<div class="child">' + x[i] + '</div>' if (i % 3 !== 0 || i === (x.length - 1)) { html += '</div>'; } } document.getElementById('test').innerHTML = html 
 .child{ margin: 10px; background: green; float: left; padding: 20px} 
 <div id="test"></div> 

you can try something like this 你可以尝试这样的事情

 var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] var html = ''; var i=0; while(i < x.length){ if (i % 3 === 0) { html += '<div class="parent">'; } html += '<div class="child">' + x[i] + '</div>' i++; if (i % 3 === 0 || i === (x.length - 1)) { html += '</div>'; } } if(i%3 !== 0){ html += '</div>'; } document.getElementById('test').innerHTML = html 
 .child{ margin: 10px; background: green; float: left; padding: 20px} 
 <div id="test"></div> 

You can loop through the array in groups of n fairly easily, using a for loop and slice : 您可以使用for循环和slice相当容易地遍历n组的数组:

 var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]; var html = ""; for (var i = 0; i < x.length; i += 4) { html += '<div class="parent">' + x.slice(i, i + 4).reduce(function(h, child) { return h + '<div class="child">' + child + '</div>'; }, "") + '</div>'; } document.getElementById("test").innerHTML = html; 
 .child{ margin: 10px; background: green; padding: 20px} .parent { border: 1px solid blue; } 
 <div id="test"></div> 

(I've removed the float from the CSS and added a parent border, just to make it obvious where the children are.) (我已经从CSS中删除了float ,并添加了父边框,以使其清楚地表明孩子在哪里。)

Note that slice(i, i + 4) will work even at the end when there aren't four left. 请注意,即使最后剩下四个时, slice(i, i + 4)也可以工作。

(The reduce is just one way to combine all those children into one string.) reduce只是将所有这些子项组合成一个字符串的一种方法。)

Using splice , you can get array of 4 elements, loop them and wrap them under the parent . 使用splice ,您可以获得4个元素的数组,将它们循环并包装在parent之下。 splice helps you chunk the big array into number of smaller arrays: splice可以帮助您将大数组分成多个较小的数组:

 var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]; var html = ''; while (x.length > 0) { chunk = x.splice(0, 4) html += '<div class="parent">'; for (var i = 0; i < chunk.length; i++) { html += '<div class="child">' + chunk[i] + '</div>' } html += '</div>'; } document.getElementById('test').innerHTML = html 
 .child { margin: 10px; background: green; padding: 20px; } .parent { border: 1px solid black; padding: 4px; margin: 5px 0; } 
 <div id="test"></div> 

Instead of starting with i=0 in the for loop start with i=1 and end with i <= x.length . 而不是在for循环中以i=0开头,以i=1开头并以i <= x.length结束。 With this in place the logic should be straight forward. 有了这个适当的逻辑应该是直截了当的。

Here is the code sample 这是代码示例

 var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] var html = ''; for (var i = 1; i <= x.length; i++) { if (i % 4 === 1) // if remainder is 1 it means the 1st element of the block, So open new parent html += '<div class="parent">'; html += '<div class="child">' + x[i - 1] + '</div>' if (i % 4 === 0 || i === x.length) // if remainder is 0 then we have completed 4 elements so close the parent html += '</div>'; } document.getElementById('test').innerHTML = html 
 .child {background: yellow;padding: 20px;display: block; flex: 1;} .parent {border: 2px solid green;margin: 10px;display: flex;} 
 <div id="test"></div> 

Edit 编辑

//if remainder is 0 then we have completed 4 elements so close the parent
//if we have reached the last element in the array then close the parent
  if (i % 4 === 0 || i === x.length) 
    html += '</div>';
}

you can change if (i % 3 !== 0 || i === (x.length - 1)) { to if (i % 3 === (3-1) || i === (x.length - 1)) { to resolve it by your idea 您可以将if (i % 3 !== 0 || i === (x.length - 1)) {更改为if (i % 3 === (3-1) || i === (x.length - 1)) {通过您的想法解决

 var x = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] var html = ''; for (var i = 0; i < x.length; i++) { if (i % 3 === 0) { html += '<div class="parent">'; } html += '<div class="child">' + x[i] + '</div>' if (i % 3 === (3-1) || i === (x.length - 1)) { html += '</div>'; } } document.getElementById('test').innerHTML = html 
 .child{ margin: 10px; background: green; float: left; padding: 20px} 
 <div id="test"></div> 

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

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