用DIV包装HTML直到下一个H3 [英] Wrap HTML with DIV until next H3

查看:104
本文介绍了用DIV包装HTML直到下一个H3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML结构:

I have the following HTML structure:

<div id="subgroup">
    <h3>Group name #1</h3>
    <a href="#">Link #1</a>
    <a href="#">Link #2</a>
    <h3>Group name #2</h3>
    <a href="#">Link #3</a>
    <a href="#">Link #4</a>
</div>

我有这个扁平结构,因为我想使用jQuery UI的手风琴效果。我想要在 h3 元素之间包装所有 a 元素。

I have this flat structure because I want to use jQuery UI's accordion effect. I want to wrap all a elements between the h3 elements.

我尝试了以下方法,但没有成功:

I tried the following, without luck:

$('#subgroup a').nextUntil('h3').wrapAll('<div></div>');

但是这造成了一些 a 元素消失。我已经尝试了很多选择器,但都没有工作。我是这样做的吗?

But this caused some of the a elements to disappear. I've tried quite a few selectors but none of them has worked. Am I doing this right?

推荐答案

像这样的方法应该可行,但我猜测有很多不同的方式这:

An approach like this should work, though I'm guessing there are many different ways of doing this:

$('#subgroup h3').each(function() {
    $(this).nextUntil('h3').wrapAll('<div></div>');
});

这篇关于用DIV包装HTML直到下一个H3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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