setAttribute問題
aa.style.fontSize="30px";
aa.setAttribute('style','color:red');
效果如下:
aa.style.color="red";
aa.setAttribute('style','font-size:30px');
效果如下:
?aa.setAttribute("style","font-size:30px");
?aa.setAttribute("style","color:red");
效果如下:
都沒有達到預期的效果。
為什么setAttribute("style","font-size:30px");類似的用法不管用,達不到效果呢??
2016-04-15
aa..width="100px";aa.setAttribute("width","200px");是后一個覆蓋了前面那一個;aa.setAttribute("style","color:red")的意思:把aa的style(行內樣式)替換成color:red,原有的全部清除;它把style當成時一個屬性一次性操作了;
<div id="aa" style="font-size:30px;color:blue;text-indent:20px;"></div> 執行aa.setAttribute("style","color:red"),就變成這樣了<div id="aa" style="color:red;"></div>;;
aa.style.fontSize="30px";的意思把style屬性中的fontSize屬性賦值為30px;
上圖修改:
1.
aa.setAttribute('style','color:red');
aa.style.fontSize="30px";
2.
aa.setAttribute('style','font-size:30px');
aa.style.color="red";
3.?
?aa.setAttribute("style","color:red;font-size:30px");
這樣應該就有你要的效果了
2016-04-15
setAttribute(); 是assgin的 前一個的內容不保留
除非你setAttribute("style","{"font-size":"30px","color":"red"}");