JS获取非行间样式及兼容问题

时间:2021-01-30 12:22:53

获取非行间样式:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取非行间样式</title>
<style>
#div1{height: 200px;width: 200px;background-color: red;}
</style>
<script>
function getStyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name]; //IE浏览器
}
else if(getComputedStyle){      //Firefox,chrome浏览器
return getComputedStyle(obj,null)[name];
}
}
window.onload = function(){
var oDiv = document.getElementById('div1');
alert(getStyle(oDiv,'height'));
} </script>
</head>
<body>
<div id="div1"></div>
</body>
</html>