js 設置css樣式的幾種方法


js動態設置樣式:

  1: 直接設置Style對象:

    el.style.color = '#b50029';
    el.style.fontSize = '30px';
    el.style['font-size'] = '30px';

    

 


  2:設置style屬性:

     【2.1】el.setAttribute('style', 'color: red;');
               el.setAttribute('style', 'font-size: 30px');

    

    【2.2】el.setAttribute('style', 'font-size: 30px');

    

    【2.3】el.setAttribute('style',
                'color: red;' + 'font-size: 30px;' + 'background-color: green');

    

  3:設置cssText

  【3.1】el.style.cssText = "color: red;font-size: 30px";

  

  【3.2】el.style.cssText = "color: red;font-size: 30px";
         el.style.cssText += 'background-color: yellow';

   

  【3.3】el.style.cssText = "color: red;font-size: 30px";
         el.style.cssText += 'background-color: yellow';
         el.style.cssText = "text-decoration: line-through";

  

  4:改變class類名 

    【4.1】el.className = 'cNone';
                el.className += 'cNtwo'; >> el[class="cNone cNtwo"]

    【4.2】el.className = 'cNone';
                el.className = 'cNtwo';      >>el[class="cNtwo"]

 

參考:
http://www.cnblogs.com/susufufu/p/5749922.html
http://javascript.ruanyifeng.com/dom/css.html
http://www.cnblogs.com/LiuWeiLong/p/6058059.html

  

  


免責聲明!

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



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