首页 > 为什么很多时候 xxx.style.height不管用?既读不到也赋值不了?

为什么很多时候 xxx.style.height不管用?既读不到也赋值不了?

xxx是变量,很奇怪xxx.style.height有的时候管用,有的时候不管用。请问是什么原因呢?
如下代码,我想控制css中#ad的height值来实现隐藏的广告缓缓展开的效果,可是oadcon.style.height根本不管用呀?根本读不到css的height值。请问是怎么回事呢?
第15行弹出为空,什么都没有。

<!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=utf-8" />
<title>无标题文档</title>
<style>
*{padding:0; margin:0;}
#ad{ width:960px; height:0px;  overflow:hidden; margin:0 auto;}
#content{width:960px; height:2000px; background-color:#FC0; margin:0 auto;}
</style>
<script>
window.onload=function (){
var oad=document.getElementById('ad');
var oadcon=document.getElementById('adcon')
alert(oad.style.height);
    }
</script>
</head>

<body>
<div id="ad">
<img id="adcon" src="3.jpg">
</div>
<div id="content"></div>
</body>
</html>


因为这样只能获取行间样式


oad.currentStyle.height;//IE下获取非行间样式

getComputedStyle(oad,false)//other


如果要读取css高度,可以使用 Window.getComputedStyle()

参考:
Window.getComputedStyle

【热门文章】
【热门文章】