jQuery / js将具有相同类的div包裹在单个div中

时间:2019-04-01 05:46:05

标签: javascript jquery html jquery-selectors

我具有以下结构,我需要将div包装在.item周围。 在某些地方,有两个项目,在某个地方,有一个项目:

<div class="section">
  <div class="heading"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="heading"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="heading"></div>
  <div class="item"></div>
  <div class="heading"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

我需要使用以下格式的输出:

<div class="section">
  <div class="heading"></div>
  <div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
  <div class="heading"></div>
  <div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
  <div class="heading"></div>
  <div>
    <div class="item"></div>
  </div>
  <div class="heading"></div>
  <div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</div>

我尝试使用以下代码,但是它将所有代码包装到单个类中。

var classes = {};
$(".section > div").each(function() {
    classes[$(this).attr("class")] = true;
});
for (singleClass in classes) {
    $("." + singleClass).wrapAll('<div class="item" />');
}

3 个答案:

答案 0 :(得分:4)

使用.heading遍历.each()个元素,并使用.nextUntil()循环选择彼此相邻的.item,然后使用.wrapAll()包装它们>

$(".heading").each(function(){
  $(this).nextUntil(".heading").wrapAll("<div></div>");
});
.heading + div {background: #ccc}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section">
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="item">item</div>
</div>

答案 1 :(得分:2)

尝试

let h,s= document.querySelector('.section');

[...s.children].forEach(e=>  {
  if(e.className=='heading') { 
    h=document.createElement("div")
    e.insertAdjacentElement('afterend',h)
  } else {
    h.appendChild(e)
  }
})

function change() {
  let h,s= document.querySelector('.section');

  [...s.children].forEach(e=>  {
    if(e.className=='heading') { 
      h=document.createElement("div")
      e.insertAdjacentElement('afterend',h)
    } else {
      h.appendChild(e)
    }
  })
}
div { margin-left: 30px;}
<button onclick="change()">Click here to change</button>

<div class="section">
  <div class="heading">head 1</div>
  <div class="item">item 1</div>
  <div class="item">item 2</div>
  <div class="heading">head 2</div>
  <div class="item">item 3</div>
  <div class="item">item 4</div>
  <div class="heading">head 3</div>
  <div class="item">item 5</div>
  <div class="heading">head 4</div>
  <div class="item">item 6</div>
  <div class="item">item 7</div>
</div>

答案 2 :(得分:-1)

  1. 将nextUntil()与warpAll()一起使用

$(".heading").each(function() {
  $(this).nextUntil(".heading").wrapAll("<div class='red'></div>")
});
.red {
  border: red 1px solid
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section">
  <div class="heading">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="heading">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
  <div class="heading">7</div>
  <div class="item">8</div>
  <div class="heading">9</div>
  <div class="item">0</div>
  <div class="item">-</div>
</div>