将元素包装在新的 div 中
我有下面的 html:
<div id="one">
<div class="two">
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
在 jQuery 中,如果我要将每 3 个元素包装在一个新的 div 中,如下所示,我该怎么做?
<div id="one">
<div class="two">
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
</div>
我尝试切片和包装,但这并没有真正帮助我。有什么想法吗?感谢您对此的帮助。
谢谢, L
I have the below html:
<div id="one">
<div class="two">
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
in jQuery if I am to wrap every 3 elemnts in a new div like below how could I do that?
<div id="one">
<div class="two">
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
</div>
I tried slice and wrap which did not really help me. Any ideas? appreciate your help on this.
Thanks,
L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://jsfiddle.net/PZkSL/
http://jsfiddle.net/PZkSL/
请参阅此 如何包装每 3 个子 div使用jquery与html?或将每3个div包裹在一个div中< /a>
Refer to this How to wrap every 3 child divs with html using jquery? or Wrap every 3 divs in a div
如何获取类为“two”的 div 元素,并遍历它的子元素。当您找到第一个孩子时,请使用 $(firstChild).before('
');在第三个孩子上使用 $(thirdChild).after('
');只需使用此方法一路迭代即可。没有使用过 .before() 或 .after() 所以不确定这将如何实现。祝你好运。
How about getting the div element with class "two", and iterate through it's children. When you find the first child use $(firstChild).before('
<div>
'); and on the third child use $(thirdChild).after('</div>
'); Just iterate all the way through using this method. Haven't used .before() or .after() so not sure how this will pan out. Good luck.我只写一个简单的
for
循环,如下所示:I would just write a simple
for
loop, something like: