如何将选定元素的多个实例包装到新的中
我需要将 div
与类 .left
和 .right
包装到一个新的 div
中。使这项工作按我的需要进行时遇到问题。
这是原始标记:
<div class="content-main">
<div class="summary" id="listing_summary_3547">
<div class="share"></div>
<div class="left"></div>
<div class="right"></div>
</div>
<div class="summary" id="listing_summary_12739">
<div class="share"></div>
<div class="left"></div>
<div class="right"></div>
</div>
<div class="summary" id="listing_summary_4">
<div class="share"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</div>
这是我需要的结果:
<div class="content-main">
<div class="summary" id="listing_summary_3547">
<div class="share"></div>
<div class="summary-inside">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
<div class="summary" id="listing_summary_12739">
<div class="share"></div>
<div class="summary-inside">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
<div class="summary" id="listing_summary_4">
<div class="share"></div>
<div class="summary-inside">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
</div>
脚本标记需要驻留在标记中(无法访问部分)。 jQuery 版本是 1.3.2
我自己能得到的最接近的是:
$('.summary .left,.summary .right').wrapAll('<div class="summary-inside"></div>')
但是结果是错误的,元素全部放在一起,而不是按照我需要的方式分布。
I need to wrap the div
's with the classes .left
and .right
into a new div
. Having problems making this work as I need it to.
This is the original markup:
<div class="content-main">
<div class="summary" id="listing_summary_3547">
<div class="share"></div>
<div class="left"></div>
<div class="right"></div>
</div>
<div class="summary" id="listing_summary_12739">
<div class="share"></div>
<div class="left"></div>
<div class="right"></div>
</div>
<div class="summary" id="listing_summary_4">
<div class="share"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</div>
This is the result I need:
<div class="content-main">
<div class="summary" id="listing_summary_3547">
<div class="share"></div>
<div class="summary-inside">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
<div class="summary" id="listing_summary_12739">
<div class="share"></div>
<div class="summary-inside">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
<div class="summary" id="listing_summary_4">
<div class="share"></div>
<div class="summary-inside">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
</div>
The script tag needs to reside within in the tags (no access to section). jQuery version is 1.3.2
The closest I am able to get on my own is:
$('.summary .left,.summary .right').wrapAll('<div class="summary-inside"></div>')
However the result is wrong, the elements get all put together instead of being distributed the way I need them to be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在已经过测试,并且确实有效:
JS Fiddle 演示。
链接演示中显示您想要的 html 的文本是使用 jQuery 1.3.2 进行操作后
.content-main
元素的更新后的 html。This is now tested, and does, indeed, work:
JS Fiddle demo.
The text in the linked demo showing the html that you want is the updated html of the
.content-main
element, after manipulation with jQuery 1.3.2.