怎么使用javascript改变颜色

怎么使用javascript改变颜色,第1张

怎么使用javascript改变颜色

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

本教程 *** 作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

使用javascript改变颜色

1、使用Style对象color 属性改变文本颜色

color 属性可设置文本的颜色。

<!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改变颜色的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/700299.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-23
下一篇 2022-04-23

发表评论

登录后才能评论

评论列表(0条)

保存