mko*_*oso 13 javascript css jquery class
我对javascript/jquery的东西很新,但我想用它做这样的事情:
我有四个这样的div并排在每个div中.现在,如果一个人有更多的内容,它会变得更高.即使他们没有那么多内容,我也希望其他div也高.所以基本上我希望脚本通过div并检查高度并将所有div高度设置为与最高div相同.希望你明白:)
Fir*_*ght 16
我在SLaks代码中遇到NaN错误.给maxHeight一个初始值为0就可以了.
var maxHeight = 0;
$('div')
.each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
.height(maxHeight);
Run Code Online (Sandbox Code Playgroud)
谢谢SLaks!
SLa*_*aks 13
您可以使用jQuery的height
方法来获取和设置元素的高度.
您需要遍历元素,找到最高的元素,然后设置所有元素的高度.
var maxHeight;
$('div')
.each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
.height(maxHeigt);
Run Code Online (Sandbox Code Playgroud)
替换'div'
为选择要均衡的元素的jQuery选择器.
小智 9
<script>
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}
$(document).ready(function() {
equalHeight($(".column"));
});
</script>
Run Code Online (Sandbox Code Playgroud)