利用javascript脚本编程使元素与容器元素等高

问题:
父元素(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定义声明更为通用。
posted on 2008-04-04 19:28  豆豆の爸爸  阅读(657)  评论(0编辑  收藏  举报