js給元素設置樣式


一、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;'

顯示效果如下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM