javascript 動態修改css樣式

來源:互聯網
上載者:User

標籤:問題   att   lin   屬性   under   一個   art   class   AC   

方法一:改變外聯css檔案,這裡不講這個。

方法二:通過改變claaName來改變樣式,文法:

obj.className = "style2";//或者obj.setAttribute("class", "style2");

方法三:使用obj.style.cssTest,文法:

 obj.style.cssText = " display:block; color:White;“

這個方法優點:

             可以這樣添加好幾個屬性。

缺點:會把obj原來的cssText清掉,解決方案:使用累加,文法:

obj.style.cssText += " display:block; color:White;“

上面cssText累加的方法在IE中是無效的。

然而,可以在前面添加一個分號來解決這個問題:

 obj.style.cssText += " ; display:block; color:White;“

注意:如果前面有樣式表檔案寫著 div { text-decoration:underline; },這個會被覆蓋嗎?不會!因為它不是直接作用於 HTML 元素的 style 屬性。

方法四: 使用obj.style.backgroundColor,文法:

obj.style.backgroundColor= "black";

注意:backgroundColor可以換成其他屬性,這裡僅是樣本,比如color,position等。

 

 

最後一個知識點:css Sprite,

就是有一張大圖,通過控制background-position來實現顯示大圖的某一部分。可以用來製作動畫。這個大圖是作為background-image:。

優點是減少http請求,缺點是定位麻煩。維護麻煩。

具體用法請移步:24181675

 

javascript 動態修改css樣式

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.