天天看點

cssText的使用(原生js添加css樣式)

參考:

https://www.cnblogs.com/majingyi/p/6840818.html

https://www.cnblogs.com/sky903700252/p/7382808.html

var head= document.getElementById("head");
head.style.width = "200px";
head.style.height = "70px";
head.style.display = "block";
           

将多行簡化成一行,減少渲染:

var head= document.getElementById("head");
head.style.cssText="width:200px;height:70px;display:bolck";
           

但cssText也有個缺點,會覆寫之前的樣式。

可以在前面添加一個分号來解決這個問題: