获取类的outerHeight并取最大值
get outerHeight of class and take the biggest value
我使用.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
DEMO
循环遍历.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'});
相关文章:
- 在Javascript数组中查找绝对最大值
- Html5输入属性的默认值,如最小值、最大值、大小等
- 按最大值获取数组索引
- 识别滑块范围的最小值和最大值
- 如何在最大值之前的某个点停止范围滑块
- 访问Highcharts系列的最小/最大值:afterSetExtremes不会激发
- 如何在拖动后获得图表的最小值和最大值放大高图表
- AngularJs:在数字输入字段中设置验证的条件最小值和最大值
- HTML5范围滑块最小值和最大值
- 如何仅在存在最小值和最大值时才显示错误消息
- 在 JavaScript 事件中添加最小值和最大值
- 从 JavaScript 中的数组中获取最大值和最小值
- 获取整数对象属性名称的最小值和最大值
- 高图表 - 在缩放重置中设置最大值和最小值
- Java 脚本 设置日期的最小值和最大值属性
- 使用javascript动态更改select中的最大值
- 如何设置jQuery微调器的最大值和最小值
- 当达到最小值和最大值时,防止在输入中显示额外的文本
- 使用函数从数组中查找最小值和最大值
- 获取类的outerHeight并取最大值