使用javascript改變顏色的方法:1、使用“元素對(duì)象.style.color = "顏色值";”語(yǔ)句來(lái)改變文本顏色;2、使用“元素對(duì)象.style.backgroundColor = "顏色值";”語(yǔ)句來(lái)改變背景顏色。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
使用javascript改變顏色
1、使用Style對(duì)象color 屬性改變文本顏色
color 屬性可設(shè)置文本的顏色。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <div id="box">元素內(nèi)容</div><br /> <button onclick="myFunction()">改變文本顏色</button> <script> function myFunction() { var box = document.getElementById("box"); box.style.color = "red"; } </script> </body> </html>
2、使用Style對(duì)象backgroundColor 屬性改變背景顏色
backgroundColor 屬性可設(shè)置元素的背景顏色。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <div id="box">元素內(nèi)容</div><br /> <button onclick="myFunction()">改變背景顏色</button> <script> function myFunction() { var box = document.getElementById("box"); box.style.backgroundColor = "red"; } </script> </body> </html>
【