2011-10-02 36 views
2

我想包裝一個<div><H2>開始直到下一個<H2>,但它只是從第一段開始。jQuery從H2中包裝一個div直到最後一段

這是我,幾乎做工作:

$('h2:contains("test")').nextUntil('h2').wrapAll('<div class="wrapper" />'); 

這裏是我的HTML:

/* from here */ 
<h2 class='test'>Include this in the wrap</h2> 
<p>this</p> 
<p>and this</p> 
<p>and this</p> /* to here */ 

<h2 class='next'>before this</h2> 

回答

4

我會嘗試:

$("h2.test").nextUntil("h2").andSelf().wrapAll('<div class="wrapper" />'); 

It does seem to do the trick.

+0

大,那有用 - 謝謝! – SparrwHawk

+0

@ SparrwHawk - David Eddings!啊!無論如何,沒問題,任何時候:) – karim79

+0

David Eddings?你不是指烏爾蘇拉勒吉恩嗎? :-P – SparrwHawk