我使用.outerHeight
来设置另一个div的高度,使用类作为选择器。
var $example = $('.example');
var $height = $example.outerHeight();
var $styles = { 'height': $height }
$('.wrapper_sub').css($styles);
我想在我网站的多个“幻灯片”中使用它:
<div class="wrapper">
<div class="example">Some Content</div>
<div class="wrapper_sub">Other Content</div>
</div>
<div class="wrapper">
<div class="example">Some Content</div>
<div class="wrapper_sub">Other Content</div>
</div>
<div class="wrapper">
<div class="example">Some Content</div>
<div class="wrapper_sub">Other Content</div>
</div>
如何获取每个.outerHeight
的{{1}},只获取最高值并将其附加到所有.example
div?
答案 0 :(得分:1)
循环遍历.example
元素并获取最大值。然后将此值应用于这些元素:
//Set an empty array
var arr = [];
//Loop through the elements
$('.example').each(function() {
//Push each value into the array
arr.push(parseFloat($(this).outerHeight()));
});
//Get the max value with sort function
var maxH = arr.sort(function(a,b) { return b-a })[0];
//Apply the max value to the '.example' elements
$('.example').css({'height': maxH + 'px'});
答案 1 :(得分:1)
查看评论内联:
var maxHeight = 0; // Initialize to zero
var $example = $('.example'); // Cache to improve performance
$example.each(function() { // Loop over all the elements having class example
// Get the max height of elements and save in maxHeight variable
maxHeight = parseFloat($(this).outerHeight()) > maxHeight ? parseFloat($(this).outerHeight()) : maxHeight;
});
$('.wrapper_sub').height(maxHeight); // Set max height to all example elements