<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#dv {
height: 200px;
width: 200px;
/*有这个属性就会被获取到,即使没有浮动*/
left: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div id="dv"></div>
<script src="common.js"></script>
<script>
//获取某个元素距离某边界的距离
function getStyle(element,attr){
//如果这个方法存在
if(window.getComputedStyle){
//谷歌支持,火狐支持
return window.getComputedStyle(element,null)[attr];
//return window.getComputedStyle(element,null).left;
//return window.getComputedStyle(element,null)["left"];
}else{
//IE8支持
return element.currentStyle[attr];
}
}
console.log(getStyle(my$("dv"),"left"));//结果为100px
</script>
</body>
</html>