日期:2014-05-16  浏览次数:20365 次

取得Css样式宽度的问题?
.w80{width:80px;}

<div   class= "w80 "   id= "m "> AAA </div>

var   e=document.getElementById( 'm ');
alert(e.style.width);       // <--------------------取到的值为null

==============================================
<div   style= "width:80px; "   id= "m "> AAA </div>

var   e=document.getElementById( 'm ');
alert(e.style.width);       // <-------------------取得的值为‘80px’


这是什么原因?我想用第一个方式取道width值,有什么办法?




------解决方案--------------------
alert(e.offsetWidth);
------解决方案--------------------
<style>
.w80{width:80px; height:20px}
</style>
function getc(){
var ec=document.styleSheets[0];
var ew=ec.rules[0];
alert(ew.style.width);//80px
}
------解决方案--------------------
class中的值无法用style获得的,楼上的可以,还可以用document.styleSheets来得到样式表
------解决方案--------------------
currentStyle
--------------------

<style>
.w80{width:80px;}
</style>
<div class= "w80 " id= "m "> AAA </div>
<SCRIPT LANGUAGE= "JavaScript ">
<!--
var e=document.getElementById( 'm ');
alert(e.currentStyle.width);
//-->
</SCRIPT>