因此,我爲每個循環遍歷每個'.related__item'並檢查它是否包含圖像(使用'.related__item-image')或不。如果爲true,則使用正確的函數計算每個「.related__item」的文本高度並觸發dotdodot函數。對每個()迭代應用函數/樣式
我的問題是,它正確地循環遍歷每個'.related__item',並且console.logs根據圖像正確的真或假。但由於某種原因,它給出了相同的高度所有的相關項目。所以我相信在if語句的某個地方出現了一些問題,但我無法弄清楚究竟是什麼。
我怎樣才能讓這個每次迭代,循環給人以真或假,設置正確的高度,所有元素然後那張旁邊,做同樣的事情。我在看每一個?
Jfiddle:https://jsfiddle.net/bwbeLbnv/
OR
JS:
var self = this;
var textBlock = $('.text', self);
var titleHeight = $('.related__item-title', self).outerHeight(true);
var containerHeight = $('.related__item', self).outerHeight();
var imageHeight = $('.related__item-image', self).outerHeight();
var relatedItems = $(".related-magazines .related__item");
var calculate_with_image = function() {
var totalHeight = titleHeight + imageHeight;
textBlock.css({height: containerHeight - totalHeight});
textBlock.trigger("update.dot");
};
var calculate_without_image = function() {
textBlock.css({height: containerHeight - titleHeight});
textBlock.trigger("update.dot");
};
var related_resize = function() {
for (var i = 0; i < relatedItems.length; i++) {
var element = relatedItems.eq(i);
var hasImage = element.find('.related__item-image', self).length === 1;
console.log($(element).text());
console.log(hasImage);
if (hasImage === true) {
calculate_with_image();
console.log('IMAGE');
} else if (hasImage === false) {
calculate_without_image();
console.log('TEXT');
}
}
};
related_resize();
HTML:(剝離)
<div class="related-magazines"> <!-- container -->
<div class="content"> <!-- inner container -->
<div>
<h3>Related pages</h3>
</div>
<!-- first column group -->
<div class="field-name-sections first">
<!-- related item w/ image -->
<div class="related__item hni-grid first">
<div class="related__item-section">
<a href="link/url">
<div class="related__item-image">
<img class="item" src="img/url">
</div>
<div class="body">
<div class="related__item-title">
<h4>
This is a title
</h4>
<h5 class="related-magazine-title">
This is a subtitle
</h5>
</div>
<div class="text">
This is a long description. etc etc etc.
</div>
</div>
</a>
</div>
</div>
<!-- related item w/o image -->
<div class="related__item last">
<div class="related__item-section">
<a href="link/url">
<div class="body">
<div class="related__item-title">
<h4>
This is a title
</h4>
<h5 class="related-magazine-title">
This is a subtitle
</h5>
</div>
<div class="text" data-padding-bottom="1">
This is a long description. etc etc etc.
</div>
</div>
</a>
</div>
</div>
</div>
<!-- second column group -->
<div class="field-name-sections last">
<!-- related item w/ image -->
<div class="related__item">
<div class="related__item-section">
<a href="link/url">
<div class="related__item-image">
<img class="item" src="image/url">
</div>
<div class="body">
<div class="related__item-title">
<h4>
This is a title
</h4>
<h5 class="related-magazine-title">
This is a subtitle
</h5>
</div>
<div class="text">
This is a long description. etc etc etc.
</div>
</div>
</a>
</div>
</div>
<!-- related item w/ image -->
<div class="related__item">
<div class="related__item-section">
<a href="link/url">
<div class="related__item-image">
<img class="item" src="image/url">
</div>
<div class="body">
<div class="related__item-title">
<h4>
This is a title
</h4>
<h5 class="related-magazine-title">
This is a subtitle
</h5>
</div>
<div class="text">
This is a long description. etc etc etc.
</div>
</div>
</a>
</div>
</div>
</div>
</div> <!-- end content --> </div> <!-- end related magazines -->
你的'HTML'在哪裏?你能分享可執行的演示/片段或[JSFiddle](https://jsfiddle.net/)嗎? [_創建一個最小,完整和可驗證的示例_](http://stackoverflow.com/help/mcve) – Rayon
@Rayon我很想提供HTML,但它是巨大的意大利麪代碼混合,因爲我正在開發一個drupal項目。 :-(我希望有人能夠幫助我完全理解邏輯,因爲每個循環都起作用,但不是if語句。 – adamk22
然後你可以解釋一下標記的基本結構嗎?這些選擇器如何「彼此相鄰」 ?只要我嘗試通過JS和jQuery調用來判斷結構,你的代碼就不會對我產生任何感覺。是否.text,'.related__item-title',...裏面或下一個地方到''.related__item'。總共有一個實例還是其中一個實例?這些節點的哪一個應該被認爲是最重要的項目?唯一對我來說很清楚的是某些*的存在(一個或多個)*這些選擇器的節點在'this'的某個地方,不管這個'this'可能是什麼。 – Thomas