JS获取当前样式函数封装:
<!DOCTYPE html>
<html>
<head>
<title>返回当前样式封装函数</title>
<style type="text/css">
div {
width: 100px;
height: 200px;
position: absolute;
top: 10px;
left: 20px;
background-color: pink;
z-index: 2;
opacity: 0.4;
}
</style>
</head>
<body>
<div id="demo"></div>
</body>
</html>
<script type="text/javascript">
var demo = document.getElementById("demo");
function getStyle(obj, attr){
if (obj.currentStyle) {
// IE浏览器识别
return obj.currentStyle[attr];
} else {
return window.getComputedStyle(obj, null)[attr];
}
}
console.log(getStyle(demo, "width"));
console.log(getStyle(demo, "background-color"));
</script>