样式属性操作

我们可以通过 JS 修改元素的大小、颜色、位置等样式

 

 

方式1:通过操作style属性

元素对象的style属性也是一个对象!

元素对象.style.样式属性 = 值;

 

<body>
    <div></div>
    <script>
        // 1. 获取元素
        var div = document.querySelector('div');
        // 2. 注册事件 处理程序
        div.onclick = function() {
            // div.style里面的属性 采取驼峰命名法 
            this.style.backgroundColor = 'purple';
            this.style.width = '250px';
        }
    </script>
</body>

  

posted @ 2022-02-09 16:50  Harry宗  阅读(47)  评论(0)    收藏  举报