首页 > 代码库 > 01-offsetWidth和offsetHeight

01-offsetWidth和offsetHeight

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        div {
            width: 100px;
            height: 100px;
            padding: 10px;
            border: 10px solid #000;
            margin: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>

    <div class="box"></div>

<script>
    
    var div = document.getElementsByTagName("div")[0];

    //offsetHeight和offsetWidth: 可以检测盒子的宽高。
    //包括宽高本身,padding,border。不包括margin

    //offsetHeight = height+padding+border;(不加margin)
    //offsetWidth = width+padding+border;(不加margin)

    console.log(div.offsetHeight);
    console.log(typeof div.offsetHeight);   //number

</script>


</body>
</html>

  

01-offsetWidth和offsetHeight