jQuery将H2的div包装到最后一段 [英] jQuery Wrap a div from H2 until last paragraph

查看:101
本文介绍了jQuery将H2的div包装到最后一段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从<H2>的开头开始包装<div>直到下一个<H2>,但是它仅从第一段开始.

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.

这就是我所拥有的,ALMOST可以完成的工作:

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

$('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>

推荐答案

我会尝试:

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

这确实可以解决问题.

这篇关于jQuery将H2的div包装到最后一段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆