使用wrapAll选择不同的元素— jQuery [英] Select different elements with wrapAll — jQuery
本文介绍了使用wrapAll选择不同的元素— jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery wrapAll将h1
标签和多个p
标签包装到一个div
中.
I am trying to wrap both a h1
tag and multiple p
tags into one div
using jQuery wrapAll.
这是我的HTML:
<h1>Title</h1>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<div class="img"></div>
还有我的jQuery:
And my jQuery:
$('h1').wrapAll('<div class="first-col" />');
$('.img').wrapAll('<div class="second-col" />');
和 JSFIDDLE .
目前,我只能包裹h1
标记,或者分别包裹h1
和p
标记.我希望它们都在一个first-col
div中.
At the moment, I can only wrap around the h1
tag, or both the h1
and p
tags separately. I want them both in the one first-col
div.
有人知道该怎么做吗?
推荐答案
尝试
$('h1').nextUntil('div.img').addBack().wrapAll('<div class="first-col" />');
演示:小提琴
这篇关于使用wrapAll选择不同的元素— jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文