我有兩個div,每個div包含兩個嵌套的div。如果其中一個嵌套div比另一個更高,我想要應用某些CSS樣式。如何將獨立的CSS樣式應用於與JS循環中的div?
我設法警報正常工作,但我一開始有點困惑的CSS樣式似乎適用於這兩種申報單。
與完成這一關任何幫助將不勝感激。
繼承人的演示https://jsfiddle.net/Lhn5uxeq/3/
HTML
<div class="slideshow" data-id="1">
<div class="slideshow-info">
<h1>
Header
</h1>
<p>
Text
</p>
</div>
<div class="slideshow-image">
<img src="https://www.w3schools.com/css/img_fjords.jpg"/>
</div>
</div>
<div class="slideshow" data-id="2">
<div class="slideshow-info">
<h1>
Header
</h1>
<p>
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text. Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text. Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text. Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text. Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text. Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text.
</p>
</div>
<div class="slideshow-image">
<img src="https://www.w3schools.com/css/img_fjords.jpg"/>
</div>
</div>
JS
$(document).ready(function(){
calculateHeight();
$(window).resize(function() {
calculateHeight();
});
});
function calculateHeight() {
$('.slideshow').each(function() {
$('.slideshow-info', this).each(function() {
slideInfoHeight = $(this).height();
});
$('.slideshow-image', this).each(function() {
slideImageHeight = $(this).height();
});
if(slideInfoHeight > slideImageHeight) {
alert('info is taller than image');
$('.slideshow-info').css('background-color', 'red');
} else {
alert ('info is shorter than image');
$('.slideshow-info').css('background-color', 'blue');
}
});
}
我已經更新了小提琴https://jsfiddle.net/Lhn5uxeq/4/ – Shiladitya