2017-06-16 45 views
0

我已經示例標記以下jQuery的如何包裝多個標籤在一個DIV

<h2>Title 1</h2> 
<input> 
<input> 
<h2>title 2</h2> 
<input> 

而且它需要輸入字段的下列

<div> 
    <h2>Title 1</h2> 
    <input> 
    <input> 
</div> 
<div> 
    <h2>title 2</h2> 
    <input> 
</div> 

的量是可變的,並且我可以在h2上設置一個類

我已經用.wrap().wrapAll()試過了,但是將每個元素包裝在一起或者每個元素分開。

有關類似問題的SO上的其他問題沒有變量輸入,可以使用next()或其他。

的div都需要在單獨的情況下手風琴,改變的東西

E: 到目前爲止: $('#id').nextUntil('.title').andSelf().wrapAll("<div class='hihi'></div>"); 工程兩個要素之一。可能需要each()

回答

0

所以我想我需要擺弄它不再發布提問之前...

$("h2").each(function(i, el){ 
    $(el).nextUntil('.title').andSelf().wrapAll("<div class='hihi'></div>"); 
}); 

爲我工作。

相關問題