例如,我有以下HTML:
<div class="TwoColumnUnit">
<div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
<div class="newsLetterDiv">
<div class="col-sm-12 col-md-12">
<div class="left_Side">
<i class="fas fas fa-route"></i>
<h3>This is about Tennis</h3>
</div>
<div class="right_Side">
<p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each. </span></p>
</div>
</div>
</div>
<div class="newsLetterDiv">
<div class="col-sm-12 col-md-12">
<div class="left_Side">
<i class="fas fas fa-basketball"></i>
<h3>This is about Basketball</h3>
</div>
<div class="right_Side">
<p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
<div class="newsLetterDiv">
<div class="col-sm-12 col-md-12">
<div class="left_Side">
<i class="fas fas fa-Football"></i>
<h3>This is about Football</h3>
</div>
<div class="right_Side">
<p><span>Association football, more commonly known as football or soccer, is a team sport played between two teams of 11 players each, who primarily use their feet to propel a ball around a rectangular field called a pitch.</span></p>
</div>
</div>
</div>
<div class="newsLetterDiv">
<div class="col-sm-12 col-md-12">
<div class="left_Side">
<i class="fas fas fa-Swimming"></i>
<h3>This is about Swimming</h3>
</div>
<div class="right_Side">
<p><span>Swimming is an individual or team racing sport that requires the use of one's entire body to move through water. </span></p>
</div>
</div>
</div>
</div>
</div>
字符串
下面是HTML的外观
的图片。
我想找到Tennis
和Football
之间的最高高度,然后将该最高高度设置为Tennis
和Football
的高度。
同样,我想找到Basketball
和Swimming
之间的最高高度,然后将该最高高度设置为Basketball
和Swimming
的高度。
我知道如何使用Math.max
找到最大高度。但如何找到每对两个div之间的最高高度?
我所尝试的:
('.TwoColumnUnit').each(function() {
var pairs = [$(this).find('.TwoColumnUnit_Left'), $(this).find('.TwoColumnUnit_Right')];
pairs.forEach(function(pair) {
if (pair.length > 0) {
var boxes = pair.find('.newsLetterDiv > div');
var maxHeight = 0;
boxes.each(function() {
$(this).css('height', '');
maxHeight = Math.max(maxHeight, $(this).height());
});
boxes.css('height', maxHeight + 'px');
}
});
});
型
这段代码的问题是,它在所有4个div之间找到最高的高度,然后将最高的高度值作为所有4个项目的高度。
但是我怎样才能找到第一个左格和右格之间的最高高度,然后在第二个左格和右格之间?
1条答案
按热度按时间ntjbwcob1#
这有点冗长,我相信有一个更好的方法比打破它像这样,但如果我理解你的问题正确,这是做这个比较的漫长的道路:
字符串
然后你必须采取赢家更新。