得到css样式
obj.style获取的是行内样式,即style属性中的值。
obj.currentStyle外部(使用)和内嵌(使用
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#demo{
width: 100px;
height: 200px;
padding: 5px;
margin: 10px;
position: absolute;
left: 15px;
top: 20px;
background-color: pink;
z-index: 2;
}
</style>
</head>
<body>
<div id="demo"></div>
</body>
</html>
<script>
var demo = document.getElementById("demo");
function getStyle(obj,attr){
if(obj.currentStyle){ //ie
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr]; // w3c
}
}
console.log(getStyle(demo,"padding"));
</script>