admin管理员组文章数量:1397102
I have an article tag that has elements inside it.
Problem: How can I wrap all elements inside a div after a certain element?
This is the current code:
<article>
<figure class="thumbnail">
<img src="src_to_img" />
</figure>
<h2>Name: Test Name</h2>
<div class="description"></div>
<div class="content"></div>
<div class="content"></div>
<div class="more"></div>
<article>
The output must be:
<article>
<figure class="thumbnail">
<img src="src_to_img" />
</figure>
<div class="description-wrap">
<h2>Name: Test Name</h2>
<div class="description"></div>
<div class="content"></div>
<div class="content"></div>
<div class="more"></div>
</div>
<article>
As you can see.. the final output has all the elements wrapped inside class="description-wrap"
after <figure></figure>
I have an article tag that has elements inside it.
Problem: How can I wrap all elements inside a div after a certain element?
This is the current code:
<article>
<figure class="thumbnail">
<img src="src_to_img" />
</figure>
<h2>Name: Test Name</h2>
<div class="description"></div>
<div class="content"></div>
<div class="content"></div>
<div class="more"></div>
<article>
The output must be:
<article>
<figure class="thumbnail">
<img src="src_to_img" />
</figure>
<div class="description-wrap">
<h2>Name: Test Name</h2>
<div class="description"></div>
<div class="content"></div>
<div class="content"></div>
<div class="more"></div>
</div>
<article>
As you can see.. the final output has all the elements wrapped inside class="description-wrap"
after <figure></figure>
-
5
$("figure ~ *").wrapAll("<div class='description-wrap'></div>")
If there are multiple, then select eacharticle
and operate on each one. – user1106925 Commented Sep 30, 2014 at 16:58 - 1 agreed with @squint, nice solution – Vahe Shadunts Commented Sep 30, 2014 at 17:00
1 Answer
Reset to default 7As @squint suggested, jQuery has a wrapAll method that can do that if bined with the next-siblings-selector ~
$("article > figure ~ *").wrapAll("<div class='description-wrap'></div>")
however, this doesn't give you the desired output when you have multiple articles. Instead we need to use .each()
like so:
$("article > figure").each(function(){
$(this).siblings().wrapAll("<div class='description-wrap'></div>")
});
.description-wrap {border:1px dotted red;}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<article>
<figure class="thumbnail">
<img src="src_to_img" />
</figure>
<h2>Name: Test Name</h2>
<div class="description"></div>
<div class="content"></div>
<div class="content"></div>
<div class="more"></div>
</article>
<article>
<figure class="thumbnail">
<img src="src_to_img" />
</figure>
<h2>Name: Test Name</h2>
<div class="description"></div>
<div class="content"></div>
<div class="content"></div>
<div class="more"></div>
</article>
本文标签: javascriptHow to wrap all elements inside a div after a certain element (JQuery)Stack Overflow
版权声明:本文标题:javascript - How to wrap all elements inside a div after a certain element? (JQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744149398a2592989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论