0
我有兩張並排的表格。我想要做的是在同一個容器中的每個表中的每行具有相同的行高度。我已經得到了這一點。使用閉包的相同行高的多個表格
它的工作方式是,你有一個數組大公抓住每個表的行高,並使用最大高度爲每一行。這很好,除了它的單個數組,這意味着如果頁面上有其他容器,他們將看同一個數組。我試着寫封閉函數但失敗了。有任何想法嗎?
$(document).ready(function() {
var heights = [];
computeTableHeights(true);
assignTableHeights();
var windowWidth = $(window).width();
$(window).resize(function() {
computeTableHeights(($(window).width() < windowWidth) && ($(window).width() != windowWidth));
windowWidth = $(window).width();
assignTableHeights();
})
function computeTableHeights(recordBiggestHeights) {
$("table").each(function() {
var rowIndex = 0;
var rows = $(this).find("tr");
$(rows).each(function() {
var rowHeight = $(this).css("height");
if (heights[rowIndex] === undefined) {
heights[rowIndex] = rowHeight;
} else {
var existingHeight = parseInt(heights[rowIndex]);
var currentHeight = parseInt(rowHeight);
if (shouldChangeHeight(recordBiggestHeights, existingHeight, currentHeight)) {
heights[rowIndex] = rowHeight;
}
}
rowIndex++;
});
});
}
function shouldChangeHeight(recordBiggestHeights, existingHeight, currentHeight) {
if (existingHeight == currentHeight) {
return false;
} else if (recordBiggestHeights) {
return existingHeight < currentHeight;
} else {
return existingHeight > currentHeight;
}
}
function assignTableHeights() {
$(".container table").each(function() {
var rowIndex = 0;
var rows = $(this).find("tr");
$(rows).each(function() {
var rowHeight = $(this).css("height");
if (heights[rowIndex]) {
var existingHeight = parseInt(rowHeight);
var targetHeight = parseInt(heights[rowIndex]);
if (existingHeight != targetHeight) {
$(this).css("height", heights[rowIndex]);
}
}
rowIndex++;
});
});
}
});
這絕對是完美的,非常感謝你的時間和精力。我仍然試圖讓我的頭在javascript中關閉和範圍。這對我幫助很大。 – mustafa
謝謝。對我而言,我已經與js合作了一段時間,並且一直使用閉包長時間,甚至不知道它們被稱爲閉包:) – renevanderark