jQuery Wrap a div from H2 until last paragraph

早过忘川 提交于 2019-12-08 03:59:47

问题


I want to wrap a <div> FROM THE BEGINNING of the <H2> up until the next <H2>, but it is only starting on the first paragraph.

This is what I have, which ALMOST does the job:

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

Here's my 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>

回答1:


I would try:

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

It does seem to do the trick.



来源:https://stackoverflow.com/questions/7628339/jquery-wrap-a-div-from-h2-until-last-paragraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!