<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>js获取非行间样式</title>
<style type="text/css">
#div2{
width: 200px;
height: 200px;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="div1" style="width: 100px; height: 100px; border: 1px solid black;"></div>
<div id="div2"></div>
</body>
</html>
<script type="text/javascript">
window.onload = function () {
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
// 1、js获取行间样式直接使用 obj.style.width 即可
console.log(div1.style.width);
// 2、js获取非行间样式
function css (obj, type) {
if (obj.currentStyle) { // 兼容IE
return obj.currentStyle[type];
} else { // 兼容火狐
return getComputedStyle(obj, false)[type];
}
}
console.log(css(div2, "width"));
}
</script>
JavaScript 获取行间样式和非行间样式
最新推荐文章于 2022-07-12 23:40:53 发布