<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 100px;
height: 100px;
line-height: 100px;
margin: 0 auto;
color: brown;
text-align: center;
background-color: skyblue;
}
</style>
</head>
<body>
<div>获取样式</div>
<script>
var div = document.querySelector('div')
//封装方法进行兼容
function getStyle(element,attribute){
//先获取需要获取样式的元素对象
var style = window.getComputedStyle?window.getComputedStyle(element):element.currentStyle
//再获取元素属性
return style[attribute]
}
console.log(getStyle(div,'width'));
</script>
</body>
</html>