javascript動態(tài)設置樣式style實例分析
本文實例講述了javascript動態(tài)設置樣式style的方法。分享給大家供大家參考。具體分析如下:
動態(tài)修改style
1.易錯:修改元素的樣式不是設置class屬性,而是className屬性.
2.易錯:單獨修改樣式的屬性使用"style.屬性名".注意在css中屬性名在javascript中
操作的時候?qū)傩悦赡懿灰粯樱饕性谀切傩悦泻?的屬性,因為
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
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>動態(tài)修改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. 示例:動態(tài)修改style(模擬開燈,關燈)
<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. 示例:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>動態(tài)設置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函數(shù)為input元素的onblur事件的響應函數(shù) inputs[i].onblur = txtOnBlur; } } } function txtOnBlur() { /* txtOnBlur是onblur的響應函數(shù),而不是被響應函數(shù)調(diào)用 的函數(shù),所有可以用this來取的發(fā)生事件控件的對象 */ 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程序設計有所幫助。
相關文章
JavaScript遍歷實現(xiàn)DFS算法和BFS算法
DFS(Depth?first?search)稱作「深度優(yōu)先遍歷」,BFS(Breadth?first?search)稱作「廣度優(yōu)先遍歷」。本文將通過JavaScript遍歷實現(xiàn)這兩種算法,需要的可以參考一下2023-01-01ES6中Array.find()和findIndex()函數(shù)的用法詳解
ES6為Array增加了find(),findIndex函數(shù)。find()函數(shù)用來查找目標元素,找到就返回該元素,找不到返回undefined,而findIndex()函數(shù)也是查找目標元素,找到就返回元素的位置,找不到就返回-1。下面通過實例詳解,需要的朋友參考下吧2017-09-09JavaScript實現(xiàn)圖片懶加載(Lazyload)
這篇文章主要介紹了JavaScript實現(xiàn)圖片懶加載(Lazyload)的相關資料,需要的朋友可以參考下2016-11-11JavaScript實現(xiàn)多維數(shù)組的方法
這篇文章主要介紹了JavaScript實現(xiàn)多維數(shù)組的方法,有需要的朋友可以參考一下2013-11-11highcharts.js數(shù)據(jù)綁定方式代碼實例
這篇文章主要介紹了highcharts.js數(shù)據(jù)綁定方式代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11第四篇Bootstrap網(wǎng)格系統(tǒng)偏移列和嵌套列
這篇文章主要介紹了Bootstrap網(wǎng)格系統(tǒng)偏移列和嵌套列的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06