关于样式的获取问题

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
input {
margin-top: 20px;
}
div {
margin-top: 30px;
width: 200px;
height: 100px;
background-color: pink;
position: absolute;
left: 0;
}
</style>
</head>
<body>
<input type="button" value="移动到400px" id="btn1" style="background-color:red;left: 0;">
<input type="button" value="移动到800px" id="btn2">
<div id="dv"></div>
<script src="common.js"></script>
<script>
//如果样式的代码是再style标签中设置的,外面(style.left)是获取不到的
//如果样式的代码是再style的属性设置的,外面是可以获取到的 style.left
console.log(document.getElementById("btn1").style.backgroundColor);//可以获取
console.log(document.getElementById("btn1").style.left);//可以获取
console.log(document.getElementById("dv").style.left);//不可以获取
console.log(document.getElementById("dv").style.backgroundColor);//不可以获取
  console.log(document.getElementById("dv").offsetLeft);//可以获取 数字类型,没有px
</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/chm-blogs/p/11307273.html