String.prototype.fontcolor()
Deprecated
String 值的 fontcolor() 方法会创建一个 <font> 元素字符串,其中嵌入了调用字符串(<font color="...">str</font>),从而导致该字符串以指定的字体颜色显示。
语法
js
fontcolor(color)
参数
返回值
一个以 <font color="color"> 开头的字符串(color 中的双引号被替换为 "),然后是文本 str,最后以 </font> 结束标记。
描述
fontcolor() 方法本身只是简单地将字符串部分连接在一起,没有进行任何验证或规范化。然而,为了创建有效的 <font> 元素,如果你将颜色表示为十六进制 RGB 三元组,你必须使用格式 rrggbb。例如,salmon(鲑鱼色)的十六进制 RGB 值为红色 = FA,绿色=80,蓝色=72,因此 salmon 的 RGB 三元组为 "FA8072"。
示例
>使用 fontcolor()
下面的示例使用 fontcolor() 方法通过生成一个带有 HTML <font> 元素的字符串来改变字符串的颜色。
js
const worldString = "Hello, world";
console.log(`${worldString.fontcolor("red")} is red in this line`);
// '<font color="red">Hello, world</font> is red in this line'
console.log(
`${worldString.fontcolor("FF00")} is red in hexadecimal in this line`,
);
// '<font color="FF00">Hello, world</font> is red in hexadecimal in this line'
使用 element.style 对象,你可以获取元素的 style 属性并进行更通用的操作,例如:
js
document.getElementById("yourElemId").style.color = "red";
规范
| 规范 |
|---|
| ECMAScript® 2027 Language Specification> # sec-string.prototype.fontcolor> |