jQuery-仅在一个容器内附加一个元素

时间:2018-10-30 13:37:03

标签: jquery append each

我有很多弹出窗口(.popmake-content),并且在每个窗口中都有两个按钮(.slick-arrow),我想将它们附加到同一弹出窗口中的另一个元素(.my_lightbox_navigation)中。 我正试图实现这样的目标:

jQuery('.popmake-content').each(function(){
    jQuery('.slick-arrow',this).appendTo('.my_lightbox_navigation',this);
});

,但是将所有.slick-arrow元素附加到.my_lightbox_navigation中。如何正确执行此操作?

1 个答案:

答案 0 :(得分:1)

尝试以下操作,appendTo没有上下文属性

jQuery('.popmake-content').each(function(i,v){ 
     var box = jQuery(v).find('.my_lightbox_navigation');
    jQuery(v).find('.slick-arrow').appendTo(box);
});

jQuery('.popmake-content').each(function(i,v){ 
     var box = jQuery(v).find('.my_lightbox_navigation');
    jQuery(v).find('.slick-arrow').appendTo(box);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="popmake-content">
    <button class="slick-arrow"> button 11</button><button class="slick-arrow"> button 12</button>
    <div class="my_lightbox_navigation"></div>
</div>
<div class="popmake-content">
    <button class="slick-arrow"> button 21</button><button class="slick-arrow"> button 22</button>
    <div class="my_lightbox_navigation"></div>
</div>
<div class="popmake-content">
    <button class="slick-arrow"> button 31</button><button class="slick-arrow"> button 32</button>
    <div class="my_lightbox_navigation"></div>
</div>
<div class="popmake-content">
    <button class="slick-arrow"> button 41</button><button class="slick-arrow"> button 42</button>
    <div class="my_lightbox_navigation"></div>
</div>
<div class="popmake-content">
    <button class="slick-arrow"> button 51</button><button class="slick-arrow"> button 52</button>
    <div class="my_lightbox_navigation"></div>
</div>