<!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>获取元素css样式</title>
<style>
#elem-container {
position: absolute;
left: 100px;
top: 200px;
height: 100px;
}
</style>
</head>
<body>
<div id="elem-container">dummy</div>
<script>
function getTheStyle() {
let elem = document.getElementById("elem-container");
let theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("height");
let theCSSprop1 = window.getComputedStyle(elem, null)['height'];
let height = parseInt(window.getComputedStyle(elem, null)['height']);
let height1 = parseInt(window.getComputedStyle(elem, null).getPropertyValue("height"));
console.log(theCSSprop, theCSSprop1, height, height1); // 100px 100px 100 100
}
getTheStyle();
</script>
</body>
</html>
js 获取元素css样式
最新推荐文章于 2022-11-02 16:54:00 发布