ntcolor() - JavaScript - MDN Web Docs
Có thể bạn quan tâm
- References
- JavaScript
- Reference
- Standard built-in objects
- String
- String.prototype.fontcolor()
- English (US)
- Remember language
- Deutsch
- Español
- Français
- 日本語
- 한국어
- Português (do Brasil)
- Русский
- 中文 (简体)
- Syntax
- Description
- Examples
- Specifications
- Browser compatibility
- See also
- Standard built-in objects
- String
- Constructor
- String() constructor
- Static methods
- String.fromCharCode()
- String.fromCodePoint()
- String.raw()
- Instance methods
- String.prototype.anchor() Deprecated
- String.prototype.at()
- String.prototype.big() Deprecated
- String.prototype.blink() Deprecated
- String.prototype.bold() Deprecated
- String.prototype.charAt()
- String.prototype.charCodeAt()
- String.prototype.codePointAt()
- String.prototype.concat()
- String.prototype.endsWith()
- String.prototype.fixed() Deprecated
- String.prototype.fontcolor() Deprecated
- String.prototype.fontsize() Deprecated
- String.prototype.includes()
- String.prototype.indexOf()
- String.prototype.isWellFormed()
- String.prototype.italics() Deprecated
- String.prototype.lastIndexOf()
- String.prototype.link() Deprecated
- String.prototype.localeCompare()
- String.prototype.match()
- String.prototype.matchAll()
- String.prototype.normalize()
- String.prototype.padEnd()
- String.prototype.padStart()
- String.prototype.repeat()
- String.prototype.replace()
- String.prototype.replaceAll()
- String.prototype.search()
- String.prototype.slice()
- String.prototype.small() Deprecated
- String.prototype.split()
- String.prototype.startsWith()
- String.prototype.strike() Deprecated
- String.prototype.sub() Deprecated
- String.prototype.substr() Deprecated
- String.prototype.substring()
- String.prototype.sup() Deprecated
- String.prototype.toLocaleLowerCase()
- String.prototype.toLocaleUpperCase()
- String.prototype.toLowerCase()
- String.prototype.toString()
- String.prototype.toUpperCase()
- String.prototype.toWellFormed()
- String.prototype.trim()
- String.prototype.trimEnd()
- String.prototype.trimStart()
- String.prototype.valueOf()
- String.prototype[Symbol.iterator]()
- Instance properties
- String: length
- Inheritance
- Object/Function
- Static methods
- Function.prototype.apply()
- Function.prototype.bind()
- Function.prototype.call()
- Function.prototype.toString()
- Function.prototype[Symbol.hasInstance]()
- Static properties
- Function: displayName Non-standard
- Function: length
- Function: name
- Function: prototype
- Function.prototype.arguments Non-standard Deprecated
- Function.prototype.caller Non-standard Deprecated
- Instance methods
- Object.prototype.__defineGetter__() Deprecated
- Object.prototype.__defineSetter__() Deprecated
- Object.prototype.__lookupGetter__() Deprecated
- Object.prototype.__lookupSetter__() Deprecated
- Object.prototype.hasOwnProperty()
- Object.prototype.isPrototypeOf()
- Object.prototype.propertyIsEnumerable()
- Object.prototype.toLocaleString()
- Object.prototype.toString()
- Object.prototype.valueOf()
- Instance properties
- Object.prototype.__proto__ Deprecated
- Object.prototype.constructor
- Syntax
- Description
- Examples
- Specifications
- Browser compatibility
- See also
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The fontcolor() method of String values creates a string that embeds this string in a <font> element (<font color="...">str</font>), which causes this string to be displayed in the specified font color.
Note: All HTML wrapper methods are deprecated and only standardized for compatibility purposes. For the case of fontcolor(), the <font> element itself has been removed from the HTML specification and shouldn't be used anymore. Web developers should use CSS properties instead.
Syntax
jsfontcolor(color)Parameters
colorA string expressing the color as a hexadecimal RGB triplet or as a string literal. String literals for color names are listed in the CSS color reference.
Return value
A string beginning with a <font color="color"> start tag (double quotes in color are replaced with "), then the text str, and then a </font> end tag.
Description
The fontcolor() method itself simply joins the string parts together without any validation or normalization. However, to create valid <font> elements, if you express color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072".
Examples
Using fontcolor()
The code below creates an HTML string and then replaces the document's body with it:
jsconst contentString = "Hello, world"; document.body.innerHTML = contentString.fontcolor("red");This will create the following HTML:
html<font color="red">Hello, world</font>Warning: This markup is invalid, because font is no longer a valid element.
Instead of using fontcolor() and creating HTML text directly, you should use CSS to manipulate fonts. For example, you can manipulate color through the element.style attribute:
jsdocument.getElementById("yourElemId").style.color = "red";Specifications
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.fontcolor |
Browser compatibility
BCD tables only load in the browser
See also
- Polyfill of String.prototype.fontcolor in core-js
- HTML wrapper methods
- <font>
Help improve MDN
Was this page helpful to you?YesNoLearn how to contribute.This page was last modified on Jul 25, 2024 by MDN contributors.
View this page on GitHub • Report a problem with this contentTừ khóa » Html Color Text Javascript
-
Change Text Color On Click Using JavaScript | Bobbyhadz
-
JavaScript String Fontcolor() Method - W3Schools
-
HTML DOM Style Color Property - W3Schools
-
Change Text Color With Javascript? - Stack Overflow
-
How To Change The Font Color Of A Text Using JavaScript?
-
How To Change Font Color Of HTML Element In JavaScript?
-
How To Change The Text Color In JavaScript - Reactgo
-
Text Color Change In Js Code Example - Code Grepper
-
JavaScript: String Fontcolor() Method - TechOnTheNet
-
- HTML: HyperText Markup Language | MDN"> - HTML: HyperText Markup Language | MDN
-
How To Change Text Color In HTML? - Top 3 Ways To Create Awesome ...
-
How To Change Text Color In Html - Javatpoint
-
How To Change Text Color On Click Using JavaScript?
-
How To Change Text Color In Javascript - YouTube