問題:
父元素(Col)內有兩個兄弟元素,分別命名為Col1和Col2
Col1或Col2中內容由服務端代碼產生,高度不用CSS設定。如何做到使兄弟元素的高度為最多的那個元素的高度?
Javascript代碼如下: 1function resizeHeight() {
2 if(!document.getElementsByTagName) return false;
3 var div = document.getElementsByTagName("div");
4 if(!div) return false;
5 for(var i=0;i<div.length;i++) {
6
7 if(div[i].parentNode.className.indexOf("Col") != -1) {
8 col = div[i].parentNode;
9 }
10 if(div[i].className.indexOf("Col1") != -1) {
11 col1 = div[i];
12
13 if(col.offsetHeight > col1.offsetHeight){
14 col1.style.height = col.offsetHeight +"px";
15 }
16 else {
17 col.style.height = col1.offsetHeight +"px";
18 }
19 }
20 else if(div[i].className.indexOf("Col2") != -1) {
21 col1 = div[i];
22
23 if(col.offsetHeight > col1.offsetHeight){
24 col1.style.height = col.offsetHeight +"px";
25 }
26 else {
27 col.style.height = col1.offsetHeight +"px";
28 }
29 }
30 }
31
32}
web文檔中用 class 定義聲明比用id定義聲明更為通用。