此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

String.prototype.fontcolor()

Deprecated

Avoid using this feature in new projects. HTML wrapper methods exist only for backwards compatibility. Use DOM APIs to create elements instead. This feature may be a candidate for removal from web standards or browsers.

Consider using the following features instead: DOM.

String 值的 fontcolor() 方法会创建一个 <font> 元素字符串,其中嵌入了调用字符串(<font color="...">str</font>),从而导致该字符串以指定的字体颜色显示。

备注:所有的 HTML 包装方法都已经被弃用,并且仅为了兼容性而标准化。对于 fontcolor() 方法来说,<font> 元素本身在 HTML5 中已被移除,不应再使用。Web 开发者应该使用 CSS 属性来代替。

语法

js
fontcolor(color)

参数

color

该参数可以接受十六进制 RGB 三元组或字符串字面量来表示颜色。颜色名称的字符串字面量可以参考 CSS 颜色参考

返回值

一个以 <font color="color"> 开头的字符串(color 中的双引号被替换为 &quot;),然后是文本 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

浏览器兼容性

参见