简体   繁体   English

如何使用普通 javascript 包装每个第 n 个 div

[英]How to wrap every n'th div using plain javascript

I have achieved this with jQuery, but I am looking for a solution with plain JavaScript.我已经用 jQuery 实现了这一点,但我正在寻找一个简单的 JavaScript 的解决方案。 I have a div structure like this -我有一个这样的 div 结构 -

<div class="parent">
    <div>content</div>
    <div>content</div>
    <div>content</div>
    <div>content</div>
    <div>content</div>
    <div>content</div>
</div>
<div class="parent">
    <div>content</div>
    <div>content</div>
    <div>content</div>
    <div>content</div>
    <div>content</div>
    <div>content</div>
</div>

... and so on

Now, I want to wrap every nth div inside every .parent div.现在,我想将每个第 n 个 div 包装在每个.parent div 中。 Like this -像这样 -

<div class="parent">
    <div class="wrapper">
        <div>content</div>
        <div>content</div>
    </div>
    <div class="wrapper">
        <div>content</div>
        <div>content</div>
    </div>
    <div class="wrapper">
        <div>content</div>
        <div>content</div>
    </div>
</div>

... and so on

Any help will be appreciated.任何帮助将不胜感激。 I have tried something like this and stuck with no further clue -我已经尝试过这样的事情并且没有进一步的线索 -

// forEach function
var forEach = function (array, callback, scope) {
    for (var i = 0; i < array.length; i++) {
      callback.call(scope, i, array[i]); // passes back stuff we need
    }
}

// select all .parent divs
var parentDivs = document.querySelectorAll('.parent');

//slicing the array
var chunk = function ( array, size ) {
  var arr = [];
  for ( var i = 0; i < array.length; i += size ) {
      var newSlicedArray = Array.prototype.slice.call( array, i, i + size );
      Array.prototype.push.apply(arr, newSlicedArray);
  }
  return arr;
}

//run foreach function
forEach(parentDivs, function (index, value) {

  var childrens = value.querySelectorAll("div");

  var final = chunk(childrens,1);
  console.log(final);

});

Your prototype call was unnecessary for the push.您的原型调用对于推送来说是不必要的。 It's a native array and not a DOM element.它是一个原生数组,而不是一个 DOM 元素。 Added code to map the arrays to arrays of DOM elements, accumulate and append them to a new DOM elements and remove from parent using reduce, and then append back to the original element. Added code to map the arrays to arrays of DOM elements, accumulate and append them to a new DOM elements and remove from parent using reduce, and then append back to the original element.

 // forEach function var forEach = function (array, callback, scope) { for (var i = 0; i < array.length; i++) { callback.call(scope, i, array[i]); // passes back stuff we need } } // select all.parent divs var parentDivs = document.querySelectorAll('.parent'); //slicing the array var chunk = function ( array, size ) { var arr = []; for ( var i = 0; i < array.length; i += size ) { var newSlicedArray = Array.prototype.slice.call( array, i, i + size ); arr.push(newSlicedArray); } return arr; } //run foreach function forEach(parentDivs, function (index, value) { var childrens = value.querySelectorAll("div"); var final = chunk(childrens,3); final.map( towrap => towrap.reduce( (acc, el) => (acc.appendChild(el),acc), document.createElement('div') ) ).forEach( el => { el.className ="wrap"; value.appendChild(el) }) });
 .wrap { border: 1px solid green }
 <div class="parent"> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> </div> <div class="parent"> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> </div>

With the following method, we count each element using querySelectorAll by using the "parent" class.使用以下方法,我们使用“父”class 使用 querySelectorAll 计算每个元素。

We then use a loop to modify each instance of the element.然后我们使用循环来修改元素的每个实例。

  1. Grab the current content inside of the parent class获取父 class 内部的当前内容
  2. Remove the current content删除当前内容
  3. Create your wrapper, and fill it with the content we stored创建你的包装器,并用我们存储的内容填充它
  4. Finally, add the wrapper inside of your parent.最后,将包装器添加到您的父级中。

 window.onload = function() { // Run after the page has loaded let p = document.querySelectorAll('.parent'); for (let i = 0; i < p.length; i++) { if (i % 2) { // select every second instance let content = p[i].innerHTML; // store the content p[i].innerHTML = ''; // remove content from inside.parent let c = document.createElement('div'); // Create the wrapper c.className = 'wrapper'; c.innerHTML = content; // Put the content inside of the wrapper p[i].appendChild(c); // Add wrapper inside of the.parent } } }
 .wrapper { border: solid 1px blue }
 <div class="parent"> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> </div> <div class="parent"> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> <div>content</div> </div>

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

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