Home >Web Front-end >JS Tutorial >Introductory example of JavaScript fontcolor method (display string according to specified color)_Basic knowledge
JavaScript fontcolor method
Thefontcolor method returns a string with color defined using the color attribute in the HTML font tag. Its syntax is as follows:
Parameter description:
参数 | 说明 |
---|---|
str_object | 要操作的字符串(对象) |
color | 必需。颜色名(red)、RGB 值(rgb(255,0,0))或者十六进制数(#FF0000) |
Tip: This method does not comply with ECMA standards and is not recommended.
fontcolor method instance
Run this example, output:
Tip: This method returns the string defined using the HTML color tag, even though this method cannot dynamically change the font color. If you want to dynamically change the font color of an element, you can refer to the following example:
Further reading: Change the font color of page elements
function changFont( x ){
var font_style = x;
var article = document.getElementById("article");
If( typeof font_style == "string" ){
article.style.color = font_style;
}
}
I am some text...
Some Text...
In this example, the color of the font (id="article") can be dynamically changed by controlling the font CSS style through JavaScript.