js操作元素的樣式


1.元素.style.屬性='屬性值'

var div=document.getElementById('box')
div.style.width='300px';
div.style.height='300px';
div.style.backgroundColor='red';   //css中有-的屬性,在js中要改成駝峰命名法

2.div.style.屬性     獲取元素的樣式

var div=document.getElementById('box')
console.log(div.style.width);
console.log(div.style.height);
console.log(div.style.backgroundColor);   //如果要通過style獲取樣式,只能獲取行內樣式

3.通過操作類,改變元素的樣式

var div=document.getElementById('box')
div.className += ' one'    //一定要記得+空格

 


免責聲明!

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



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