【发布时间】:2015-03-17 05:05:20
【问题描述】:
我想调整三列 div 的大小,使它们的长度都相同。我已经能够通过执行以下函数在页面加载时做到这一点(我在 SO 上找到了它,但不幸的是我现在无法找到它来给出归属,如果我能找到它会更新):
function resizeIt()
{
var largest = 0;
$(".feature").each(function(){ //loop through each section
var findHeight = $(this).height(); //find the height
if(findHeight > largest){ //see if this height is greater than "largest" height
largest = findHeight; //if it is greater, set largest height to this one
}
});
$(".feature").css({"height":largest+"px"});
}
这很好用,但我也希望每次调整窗口大小时都调整 div 的大小。所以我修改了该函数,然后在每次窗口调整大小时调用它,然后在页面加载时调用该函数。这是修改后的函数(第一行函数是加法)加上调整窗口大小的函数调用:
function resizeIt()
{
$(".feature").css({"height: auto"});
var largest = 0;
$(".feature").each(function(){ //loop through each section
var findHeight = $(this).height(); //find the height
if(findHeight > largest){ //see if this height is greater than "largest" height
largest = findHeight; //if it is greater, set largest height to this one
}
});
$(".feature").css({"height":largest+"px"});
}
resizeIt();
$(window).resize(function(){
resizeIt();
});
现在我的 div 在页面加载或页面调整大小时无法正确调整大小。什么都没有发生。如果我在我的函数中加入某种警报,当
$(".feature").css({"height: auto"});
line 包含在函数中。为什么这一行会破坏我的代码?当我在第一行调用它时,该对象确实存在,因此它不是空对象。
【问题讨论】:
-
应该是
$(".feature").css({"height": "auto"}); -
@LShetty 就是这样。谢谢。
标签: javascript jquery html css resize