0

我有以下 html 结构(这只是一个概述,所有 div 都有内容并正确关闭)。每个 div.views-row 包含一个 views-field-title、views-field-body 和 views-field-afbeeldingen

<div class="view view-toepassingen view-id-toepassingen view-display-id-page view-dom-id-5a6ac8323a7566e5f11218e7b6c49c5c">
    <div class="view-content">
    <div class="views-row views-row-1 views-row-odd views-row-first">
        <div class="views-field views-field-title">
        <div class="views-field views-field-body">
        <div class="views-field views-field-field-afbeeldingen">
    </div>
    <div class="views-row views-row-2 views-row-even">
    <div class="views-row views-row-3 views-row-odd">
    <div class="views-row views-row-4 views-row-even">
    <div class="views-row views-row-5 views-row-odd">
    <div class="views-row views-row-6 views-row-even views-row-last">
</div>

我想要实现的是将 div 重新排列为:(有效地将标题和正文 div 移动到 'afbeeldingen' div 中,并且对于每个视图行)

<div class="view view-toepassingen view-id-toepassingen view-display-id-page view-dom-id-5a6ac8323a7566e5f11218e7b6c49c5c">
    <div class="view-content">
    <div class="views-row views-row-1 views-row-odd views-row-first">
        <div class="views-field views-field-field-afbeeldingen">
            <div class="views-field views-field-title">
            <div class="views-field views-field-body">
        </div>
    </div>
    <div class="views-row views-row-2 views-row-even">
    <div class="views-row views-row-3 views-row-odd">
    <div class="views-row views-row-4 views-row-even">
    <div class="views-row views-row-5 views-row-odd">
    <div class="views-row views-row-6 views-row-even views-row-last">
</div>

这是我的行为不端的 jquery 代码。我正在尝试遍历所有 6 个视图行 div。

var i = 1;
$('.view-toepassingen > .view-content').children('div').each(function () {
    $('.view-toepassingen > .view-content > .views-field-field-afbeeldingen').prepend($('.view-toepassingen > .view-content > .views-row-'+i +' .views-field-title'));
    $('.view-toepassingen > .view-content > .views-field-field-afbeeldingen').prepend($('.view-toepassingen > .view-content > .views-row-'+i +' .views-field-body'));
    i++;
});
4

2 回答 2

0

.each回调内部,this指的是当前div项目。您可以使用$(this).find()搜索此节点的子节点:

$('.view-toepassingen > .view-content').children('div').each(function () {
    var $title = $(this).find('.views-field-title');
    var $body = $(this).find('.views-field-body');

    $(this).find('.views-field-field-afbeeldingen').append($title, $body);
});

.find()将查看该节点的所有子节点(在任何深度),.children()只会查看其直接子节点,等等...

于 2013-06-26T14:13:18.383 回答
0

each()遍历afbeeldingendiv 并搜索和移动每个 div 的兄弟姐妹可能更有意义:

$(".views-field-field-afbeeldingen").each(function() {
    $(this).siblings(".views-field-title").appendTo(this);
    $(this).siblings(".views-field-body").appendTo(this);
});

另请注意,您拥有的所有手动奇数、偶数、第一个、最后一个类都可以使用适当的伪类更容易地实现。

看到这个小提琴

于 2013-06-26T14:19:42.087 回答