JavaScript fontcolor method entry instance (to display strings in the specified color)

  • 2020-03-30 04:09:02
  • OfStack

JavaScript fontcolor method

The fontcolor method returns the colored string defined using the color attribute in the HTML font tag. The syntax is as follows:


str_object.fontcolor( color )

Parameter description:

parameter instructions
str_object String (object) to manipulate
color A necessity. The color of (red) , RGB value (rgb(255,0,0)) Or hexadecimal Numbers (#FF0000)

Note: this method does not conform to ECMA standards and is not recommended.

Fontcolor method instance


<script language="JavaScript"> var str = "www.jb51.net";
document.write( str.fontcolor("red") ); </script>

Run the example and output:


www.jb51.net

Tip: this method returns a string defined using the HTML color tag, which does not dynamically change the color of the font. If you want to change the font color of an element dynamically, see the following example:

Read more: change the font color of page elements


<html> <script language="JavaScript"> function changFont( x ){
    var font_style = x;
    var article = document.getElementById("article");
    if( typeof font_style == "string" ){
        article.style.color = font_style;
    }
} </script> <body>
<p>
<a onClick="changFont('black');"> black </a> <a onClick="changFont('red');"> red </a>
<a onClick="changFont('blue');"> blue </a>
</p>
<p id="article">
I am some words ...<br />
Some Text ...
</p>
</body>
</html>

In this example, you can dynamically change the color of the font (id="article") by using JavaScript to control the CSS style of the font.


Related articles: