如何将所选元素的多个实例包装到新的< div>中 [英] How to wrap multiple instances of selected elements into new <div>'s

查看:75
本文介绍了如何将所选元素的多个实例包装到新的< div>中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将具有类.left.rightdiv包装到新的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.

这是原始标记:

<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

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.

推荐答案

现在已经过测试,并且确实可以正常工作:

This is now tested, and does, indeed, work:

$('.content-main .summary').each(
    function(){
        $(this).find('.left,.right').wrapAll('<div class="summary-inside"></div>');
    });

JS Fiddle演示.

链接的演示中显示所需html的文本是在使用jQuery 1.3.2进行操作后.content-main元素的更新的html.

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.

这篇关于如何将所选元素的多个实例包装到新的&lt; div&gt;中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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