我有以下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重新排列为:(有效地移动'afbeeldingen'div内的标题和正文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++;
});
答案 0 :(得分: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()
只会查看其直接子女等...
答案 1 :(得分:0)
在each()
div上执行afbeeldingen
并搜索并移动每个div的兄弟姐妹可能更有意义:
$(".views-field-field-afbeeldingen").each(function() {
$(this).siblings(".views-field-title").appendTo(this);
$(this).siblings(".views-field-body").appendTo(this);
});
另请注意,使用适当的pseudo classes可以更轻松地实现您拥有的所有手动奇数,偶数,第一,最后一个课程。
请参阅this fiddle。