JavaScript-218:offset与style的区别

在这里插入图片描述

效果图

在这里插入图片描述

结构

    <div class="box" style="width:200px"></div>

CSS

        .box {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            padding: 10px;
        }

JS

        // offset 与style的区别
        var box = document.querySelector('.box');
        console.log(box.offsetWidth);
        console.log(box.style.width);
        box.style.width = '300px';
        // 获取大小用offset 给值用style

猜你喜欢

转载自blog.csdn.net/chuan0106/article/details/123399998