使用javascript改变颜色的方法:1、使用“元素对象.style.color = "颜色值";”语句来改变文本颜色;2、使用“元素对象.style.backgroundColor = "颜色值";”语句来改变背景颜色。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
使用javascript改变颜色
1、使用Style对象color 属性改变文本颜色
color 属性可设置文本的颜色。
立即学习“Java免费学习笔记(深入)”;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="box">元素内容</div><br />
<button onclick="myFunction()">改变文本颜色</button>
<script>
function myFunction() {
var box = document.getElementById("box");
box.style.color = "red";
}
</script>
</body>
</html>
2、使用Style对象backgroundColor 属性改变背景颜色
backgroundColor 属性可设置元素的背景颜色。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="box">元素内容</div><br />
<button onclick="myFunction()">改变背景颜色</button>
<script>
function myFunction() {
var box = document.getElementById("box");
box.style.backgroundColor = "red";
}
</script>
</body>
</html>
【相关推荐:javascript学习教程】
以上就是怎么使用javascript改变颜色的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号