donglv9116 2019-03-20 23:39
浏览 114
已采纳

如何在jquery中递归地将所有子元素传递给组div包装器?

For the code below I want each et_pb_module to transfer to div with class of item recursively.

<div class="et_pb_column">

    <div class="et_pb_module">
        content 1
    </div>

    <div class="et_pb_module">
        content 2
    </div>

    <div class="et_pb_module">
        content 3
    </div>

</div>


<div class="owl-wrapper">
    <div class="owl-item">
        <div class="item">
        </div>
    </div>

    <div class="owl-item">
        <div class="item">
        </div>
    </div>

    <div class="owl-item">
        <div class="item">
        </div>
    </div>

</div>

The correct output looks like this:

<div class="owl-wrapper">
    <div class="owl-item">
        <div class="item">

            <div class="et_pb_module">
                content 1
            </div>

        </div>
    </div>

    <div class="owl-item">
        <div class="item">

            <div class="et_pb_module">
                content 2
            </div>

        </div>
    </div>

    <div class="owl-item">
        <div class="item">

            <div class="et_pb_module">
                content 3
            </div>

        </div>
    </div>

</div>

My current jquery code looks like this:

  $(".et_pb_column > .et_pb_module").each(function() {
    $("#owl-demo .owl-item > .item").append($(this).html());
  });

The problem with my jquery code is that it copes all et_pb_module to each div with class of item

Any help is appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • dongyang2229 2019-03-20 23:47
    关注

    This is what you are looking to do:

    $(".et_pb_column > .et_pb_module").each(function(i) {
        $(".owl-item > .item")[i].append($(this)[0]);
    });
    

    Here is the working code:

     $(".et_pb_column > .et_pb_module").each(function(i) {
        $(".owl-item > .item")[i].append($(this)[0]);
      });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="et_pb_column">
    
        <div class="et_pb_module">
            content 1
        </div>
    
        <div class="et_pb_module">
            content 2
        </div>
    
        <div class="et_pb_module">
            content 3
        </div>
    
    </div>
    
    
    <div class="owl-wrapper">
        <div class="owl-item">
            <div class="item">
            </div>
        </div>
    
        <div class="owl-item">
            <div class="item">
            </div>
        </div>
    
        <div class="owl-item">
            <div class="item">
            </div>
        </div>
    
    </div>

    Inspect to see the set is correct.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历