function getStyle(obj,attr){
return obj.currentStyle? obj.currentStyle[attr] : getComputedStyle(obj,null)[attr];
}
</script>
IE Opera 支持currentStyle
firefox Safari支持getComputedStyle
为了让浏览器兼容
obj.currentStyle? obj.currentStyle[attr] : getComputedStyle(obj,null)[attr]
例子:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Screen</title>
<style type="text/css">
.test2{
width:100px;
height:200px;
background-color:green;
border-bottom:1px solid black;
}
.test1{
width:100px;
height:200px;
background-color:red;
border-bottom:1px solid black;
}
</style>
<script type="text/javascript">
function getStyle(obj,attr){
return obj.currentStyle? obj.currentStyle[attr] : getComputedStyle(obj,null)[attr];
}
function t(){
var div=s=document.getElementsByTagName('div')[0];
if(div.className.indexOf('test1')>=0){
div.className='test2'
}else{
div.className='test1'
}
div.style.width=parseInt(getStyle(div,'width'))+5+"px";
div.style.height=parseInt(getStyle(div,'height'))+5+"px";
div.style.borderBottomWidth=parseInt(getStyle(div,'borderBottomWidth'))+1+"px";
//alert(document.getElementById('div1').style.width);
}
</script>
</head>
<body>
<div class="test1" οnclick="t()" id="div1">
点击div,使其背景色红绿jiaoti
款高增加5px
下边框增粗1px
</div>
</body>
</html>