javascript - 将元素移入另一个(具有相同类的父元素)

标签 javascript jquery html

我正在尝试将一个元素移动到同一父元素中的另一个元素中 那个 parent 有同一个类(class)的 sibling ,所以我想把这个应用到每个 sibling 身上,但只适用于他们的 child

目前它获取所有 title 并将它们移动到 image 中,所以我最终在每个元素上有 3 个 title

我想要的是只移动 image 中每个 itemtitle

<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/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'addEventListener' of undefined at app. js:4

php - 为什么在使用 php 将 CSV 文件导入 MySQL 时会得到重复数据

javascript - 响应代理Js

javascript - JSP 和 Jquery 中的美元大括号问题

php - json_encode 创建格式错误的 JSON 数据?

javascript - 如何在javascript中查找内存泄漏

javascript - 动画一个绝对定位的 child 溢出的容器

python - 使用 html5 运行 python 代码

javascript - div 不会在图像悬停时显示

javascript - 如何使用MongoDB在Meteor中连续发布最新的N条记录?