css函數,來自於jQuery,擷取非行間樣式

來源:互聯網
上載者:User

標籤:

css(oDiv,‘width‘)擷取樣式(會把樣式的結果給返回出去)

css(oDiv,‘width‘,‘200px‘)       設定樣式

簡單來說,css函數給兩個參數是擷取,給三個參數是設定。

html格式:

  1.   <body>  
  2.     <div id="div1" style="width:200px; height:200px; background:red;">  
  3.     </div>    

js代碼:

  1. function css()  
  2.     {  
  3.         if(arguments.length==2)  
  4.         {  
  5.             return arguments[0].style[arguments[1]];  
  6.         };  
  7.         else  
  8.         {  
  9.             return arguments[0].style[arguments[1]]=arguments[2];  
  10.         };  
  11.     };  
  12.   
  13.     window.onload=function ()  
  14.     {  
  15.         var oDiv=document.getElementById(‘div1‘);  
  16.   
  17.         //alert(css(oDiv,‘width‘));  //兩個參數,擷取樣式  
  18.   
  19.         css(oDiv,‘background‘,‘green‘);  //三個參數,設定樣式  
  20.     };  

上面的代碼簡寫:

  1. function css(obj,name,value)  
  2.     {  
  3.         //alert(obj==arguments[0]);  
  4.         if(arguments.length==2)  
  5.         {  
  6.             return obj.style[name];  
  7.         };  
  8.         else  
  9.         {  
  10.             return obj.style[name]=value;  
  11.         };  
  12.     };  
  13.   
  14.     window.onload=function ()  
  15.     {  
  16.         var oDiv=document.getElementById(‘div1‘);  
  17.   
  18.         alert(css(oDiv,‘width‘));  
  19.   
  20.         //css(oDiv,‘background‘,‘green‘);  
  21.     };  

 

css函數,來自於jQuery,擷取非行間樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.