javascript動態設定樣式style執行個體分析

來源:互聯網
上載者:User

   本文執行個體講述了javascript動態設定樣式style的方法。分享給大家供大家參考。具體分析如下:

  動態修改style

  1.易錯:修改元素的樣式不是設定class屬性,而是className屬性.

  2.易錯:單獨修改樣式的屬性使用"style.屬性名稱".注意在css中屬性名稱在javascript中

  操作的時候屬性名稱可能不一樣,主要集中在那些屬性名稱中含有-的屬性,因為

  javascript中-是不能做屬性,類名的。所以在CSS中背景色是background-clolor,而javascript中則是style.background;元素樣式名是class,在javascript中是className屬性;font-size->style.fontSize;margin-top->style.marginTop

 3.單獨修改控制項的樣式<input type="button" value="AAA" onclick="this.style.color='red'" />

  1.舉例1

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>動態修改style</title> <style type="text/css"> .day { background-color:Green; } .night { background-color:Black; } </style> <script type="text/javascript"> function dayclick() { var divMain = document.getElementById("divMain"); //注意這裡使用的是className而不是class divMain.className = "day"; } function nightclick() { var divMain = document.getElementById("divMain"); divMain.className = "night"; } </script> </head> <body> <div id="divMain" class="day"> <font color="red">中華人名共和國</font> </div> <input type="button" value="白天" onclick="dayclick()" /> <input type="button" value="黑夜" onclick="nightclick()" /> </body> </html>

  2. 樣本:動態修改style(類比開燈,關燈)

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> .day { background-color:White; } .night { background-color:Black; } </style> <script type="text/javascript"> function switchLight() { var btnSwitch = document.getElementById("btnSwitch"); if (document.body.className == "day") { document.body.className = "night"; btnSwitch.value = "開燈"; } else { document.body.className = "day"; btnSwitch.value = "關燈"; } } </script> </head> <body class="night"> <input type="button" value="開燈" id="btnSwitch" onclick="switchLight()"/> </body> </html>

  3. 樣本:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>動態設定style練習(修改文字框背景色)</title> <script type="text/javascript"> function IniEvents() { var inputs = document.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == "text") { //設定txtOnBlur函數為input元素的onblur事件的響應函數 inputs[i].onblur = txtOnBlur; } } } function txtOnBlur() { /* txtOnBlur是onblur的響應函數,而不是被響應函數調用 的函數,所有可以用this來取的發生事件控制項的對象 */ if (this.value.length <= 0) { this.style.background = "red"; } else { this.style.background = "white"; } } </script> </head> <body onload="IniEvents()"> <input type="text" /><br /> <input type="text" /><br /> <input type="text" /><br /> <input type="text" /><br /> <input type="text" /><br /> <input type="text" /><br /> </body> </html>

  希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.