我很好地利用了我想出的这个小功能
function getH(id)
{
return document.getElementById(id).offsetHeight;
}
// I have a styles.js file where this function feels right at home. Access this function from anywhere.
这将返回给该函数的任何给定元素的高度(通过其 ID)。现在我们已经完成了 1/3。
然后使用 Math.max() 函数获取最大元素的高度。
var maxH = Math.max(getH("ID1"),getH("ID2"));
这是 2/3,YAY - 现在将元素高度设置为相同。
var x = document.getElementById("ID1");
var y = document.getElementById("ID2");
x.style.height = maxH +"px";
y.style.height = maxH +"px"; //Remember the +"px" has to be added as a string, thats the reason for the "".
完毕!! - 现在把它们放在一起
我会想象这样的事情
function setElementHeight()
{
var maxH = Math.max(getH("ID1"),getH("ID2"));
var x = document.getElementById("ID1");
var y = doc...("ID2");
x.style.height = maxH +"px";
y.style.height = maxH +"px";
}
// Don't forget to include the first function in you'r .js file
这会将两个 ID 设置为相同的高度,并且高度将等于最高的高度。这就是你想要的,不是吗?
--EDIT--
如果我是你,我就会扔掉阵列。只需让 2 个 DIV 各有一个唯一的 ID,并据此使它们高度相等即可。