如何用外容器包裹手风琴对?
我的手风琴有以下问题。我有一对带有外部容器“手风琴”的元素,但我需要用另一个容器包装每一对。据我了解,我以前无法包裹它们,因为手风琴不起作用。
所以我需要在 domready 之后用一个额外的片段包裹它们...
我得到了这个:
<div id="accordion">
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
....more pairs
</div>
我需要这个:
<div id="accordion">
<div class="outer">
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
</div>
<div class="outer">
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
</div>
...more pairs
</div>
我认为这可以完成工作:
$('.head').before('<div class="outer">');
$('.content').after('</div>');
...但它在每个标题之前插入已经关闭的 div。
I have the following problem with the accordion. I have pairs of elements with an outer container "accordion" but I need to wrap each pair with another container. As I understood I can't wrap them before because the accordion won't work.
So I need to wrap them after domready with an additional snippet...
I got this:
<div id="accordion">
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
....more pairs
</div>
I need this:
<div id="accordion">
<div class="outer">
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
</div>
<div class="outer">
<h2 class="head">Headline</h2>
<div class="content">Some content...</div>
</div>
...more pairs
</div>
I thought this will do the job:
$('.head').before('<div class="outer">');
$('.content').after('</div>');
...but it inserts already closed divs before each headline.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能使用
before()
和after()
等方法插入整个元素。实现您想要的效果的一种方法是在每个
上调用 wrapAll() ;
/对,使用类似 nextUntil()< /a> 到匹配对:
You can only insert whole elements using methods like
before()
andafter()
.One way to achieve what you want would be to call wrapAll() on each
<h2>
/<div>
pair, using something like nextUntil() to match the pairs:试试这个
JSFiddle 示例
Try this
JSFiddle Example