一、style
利用 “[元素].style.[CSS屬性名] = [屬性值]” 的方法
1 var Box = document.getElementById('box') 2 Box.style.height = '100px' 3 Box.style.width = '100px' 4 Box.style.backgroundColor = "red"
顯示效果如下
二、style.cssText
此方法很簡潔,更像寫行內樣式
利用 [元素].style.cssText = 'CSS樣式' 的方法
1 var Box = document.getElementById('box') 2 3 Box.style.cssText = 'height: 100px; width: 100px; background-color: darkblue;'
顯示效果如下