String.prototype.fontcolor()
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.
String
值的 fontcolor()
方法创建一个字符串,将该字符串嵌入到 <font>
元素 (<font color="...">str</font>
) 中,从而导致该字符串以指定的字体颜色显示。
¥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.
注意:所有 HTML 封装方法 均已弃用,仅出于兼容性目的而进行标准化。对于
fontcolor()
的情况,<font>
元素本身已从 HTML 规范中删除,不应再使用。Web 开发者应该使用 CSS 属性。¥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.
语法
参数
返回值
描述
¥Description
fontcolor()
方法本身只是将字符串部分连接在一起,而不进行任何验证或规范化。但是,要创建有效的 <font>
元素,如果将颜色表示为十六进制 RGB 三元组,则必须使用格式 rrggbb
。例如,鲑鱼的十六进制 RGB 值为红色=FA、绿色=80 和蓝色=72,因此鲑鱼的 RGB 三元组为 "FA8072"
。
¥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"
.
示例
使用字体颜色()
¥Using fontcolor()
下面的代码创建一个 HTML 字符串,然后用它替换文档的正文:
¥The code below creates an HTML string and then replaces the document's body with it:
const contentString = "Hello, world";
document.body.innerHTML = contentString.fontcolor("red");
这将创建以下 HTML:
¥This will create the following HTML:
<font color="red">Hello, world</font>
警告:此标记无效,因为
font
不再是有效元素。¥Warning: This markup is invalid, because
font
is no longer a valid element.
你应该使用 CSS 来操作字体,而不是使用 fontcolor()
直接创建 HTML 文本。例如,你可以通过 element.style
属性操作 color
:
¥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:
document.getElementById("yourElemId").style.color = "red";
规范
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.fontcolor |
浏览器兼容性
BCD tables only load in the browser