var box = document.getElementById('box')
console.log(box.style.width) // 获取方式1
console.log(window.getComputedStyle(box, null).width) // 获取方式2
console.log(window.getComputedStyle(box, null).getPropertyValue('width')) // 获取方式3
// 2和3中的null是伪类的位置比如 ‘:before’
setTimeout(function () {
box.style.backgroundColor = 'red' // 设置方式1
setTimeout(function () {
box.style.cssText = 'width: 200px;height: 200px;background-color:green;' // 设置方式2 完全覆盖
}, 1000)
}, 1000)
javascript --获取和设置css
最新推荐文章于 2022-10-09 00:00:00 发布