我使用 .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>
如何获取每个 .example
的 .outerHeight
,只取最高值并将其附加到所有 。 wrapper_sub
div?
最佳答案
查看内联评论:
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
关于javascript - 获取类的outerHeight并取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31109086/