<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>js中获取元素自身样式</title>
<style type="text/css">
div{
width:100px;
height:100px;
border:4px solid #000;
background:pink;
}
</style>
<script>
function getStyle(obj,attr){ //两个参数:对象和属性
if (obj.currentStyle) {
return obj.currentStyle[attr];//ie
}else{
return getComputedStyle(obj,false)[attr];//ff
};
};
window.onload=function (){
var oDiv=document.getElementById('div1');
alert(getStyle(oDiv, 'width'));
}
</script>
</head>
<body>
<div></div>
</body>
</html>
转载于:https://my.oschina.net/u/2617389/blog/608113