日期:2014-05-17  浏览次数:20696 次

js 获取css 样式
<!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 type="text/css">
	#div-01{width:100px; height:100px; border:solid 10px #000; background:#eee;/* padding:10px; margin:10px;*/}
</style>
</head>
<body>
	<div id="div-01"></div>
</body>
</html>
<script type="text/javascript">
	//获取浏览器的渲染模式
	alert(document.compatMode);
	//通过js获取css的属性值
	//内联样式document.getElementById().style.width;<div style='width:100px; height:100px; border:solid 10px #000; background:#eee;'></div>
	function getStyle(obj,attr){   
        if(obj.currentStyle){   
            return obj.currentStyle[attr];   
        }   
        else
		{   
            return document.defaultView.getComputedStyle(obj,null)[attr];   
        }   
	}  
	var bb=document.getElementById('div-01');
	alert(getStyle(bb,'width'));
</script>