Home >Web Front-end >JS Tutorial >How to change font color in JavaScript

How to change font color in JavaScript

青灯夜游
青灯夜游Original
2021-07-20 16:42:5625232browse

In JavaScript, you can use the color property of the Style object to modify the font color. This property is used to set the color of the font text (the foreground color of the element); the syntax format is "element object.style.color=" Color value ";".

How to change font color in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the color property of the Style object to modify the font color.

Let’s take a look at the example below:

<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript">
			function setColor() {
				document.getElementById("p1").style.color = "#ff0000";
				document.getElementById("p2").style.color = "magenta";
			}
		</script>
	</head>
	<body>

		<p id="p1">这是一个示例段落。</p>
		<p id="p2">这也是一个示例段落。</p>
		<input type="button" onclick="setColor()" value="改变字体颜色" />

	</body>
</html>

Rendering:

How to change font color in JavaScript

Description: The

color property sets the color of the text (the foreground color of the element).

Syntax:

Object.style.color=color

Possible values

Value Description
color

The color value can be a color name, rgb value or hexadecimal number, etc.

Default value: Depends on browser.

Related recommendations: "How to set the background color in javascript"

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to change font color in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn