<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
#test{
width:100px; height:100px; background-color:#CCCCCC;
}
</style>
</head>
<body>
<div id="test"></div>
<script>alert(test.style.height);</script>
</body>
</html>如上述代码,如何才能获取到这个DIV的高度?
Ps:如果是行内样式侧可以获取到,其他类型的就都不行!怎搞?

解决方案 »

  1.   

    试试:document.getElementById("test").offsetHeight
      

  2.   

    function getCurrentStyle(obj) {
        if (obj.currentStyle)// IE
            return ele.currentStyle;
        else// DOM兼容浏览器
            return document.defaultView.getComputedStyle(obj, null);
    }alert(getCurrentStyle(document.getElementById("test")).height);
      

  3.   

    上面打错个变量名。
    function getCurrentStyle(obj) {
        if (obj.currentStyle)// IE
            return obj.currentStyle;
        else// DOM兼容浏览器
             return document.defaultView.getComputedStyle(obj, null);
    }alert(getCurrentStyle(document.getElementById("test")).height);