我正在尝试将一个元素移动到同一父元素中的另一个元素中 那个 parent 有同一个类(class)的 sibling ,所以我想把这个应用到每个 sibling 身上,但只适用于他们的 child
目前它获取所有 title
并将它们移动到 image
中,所以我最终在每个元素上有 3 个 title
。
我想要的是只移动 image
中每个 item
的 title
<div class="item">
<a href="" class="image">
<img src="">
</a>
<h3 class="title"></h3> <!-- Element to move inside 'image' -->
</div>
<div class="item">
<a href="" class="image">
<img src="">
</a>
<h3 class="title"></h3>
</div>
<div class="item">
<a href="" class="image">
<img src="">
</a>
<h3 class="title"></h3>
</div>
这就是我一直在尝试的...
$( ".item" ).each(function( index ) {
$(this).find('.title').prependTo('.image');
});
最佳答案
//find the title and image relative to the item being iterated over
$( ".item" ).each(function( index ) {
$('.title', this).prependTo($('.image', this));
});
关于javascript - 将元素移入另一个(具有相同类的父元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53655159/