Skip to content

Commit

Permalink
[zh-cn]: Update translation of String.fontColor() (#15317)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: A1lo <[email protected]>
  • Loading branch information
3 people committed Aug 22, 2023
1 parent 0807330 commit d0f0417
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,54 @@ slug: Web/JavaScript/Reference/Global_Objects/String/fontcolor

{{JSRef}} {{deprecated_header}}

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

> **备注:** \<font> 元素已经在在[HTML5 中](/zh-CN/docs/Web/Guide/HTML/HTML5)被移除并且不应该在使用。替代的是,Web 开发者应该使用[CSS](/zh-CN/docs/Web/CSS)属性
> **备注:** 所有的 [HTML 包装方法](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#html_包装器方法)都已经被弃用,并且仅为了兼容性而标准化。对于 `fontcolor()` 方法来说,`<font>` 元素本身在 [HTML5](/zh-CN/docs/Glossary/HTML5) 中已被移除,不应再使用。Web 开发者应该使用 [CSS](/zh-CN/docs/Web/CSS) 属性来代替
## 语法

```plain
str.fontcolor(color)
```js-nolint
fontcolor(color)
```

### 参数

- `color`
- : 代表颜色的一个字符串,可以是三个一组的十六进制的 RGB 值,也可以是一个颜色名称的字符串字面量,颜色名称的字符串字面量被列在了这里 [CSS 颜色参考](/zh-CN/docs/Web/CSS/color_value)
- : 该参数可以接受十六进制 RGB 三元组或字符串字面量来表示颜色。颜色名称的字符串字面量可以参考 [CSS 颜色参考](/zh-CN/docs/Web/CSS/color_value)

### 返回值

一个包含一个{{HTMLElement("font")}} HTML 元素的字符串
一个以 `<font color="color">` 开头的字符串(`color` 中的双引号被替换为 `&quot;`),然后是文本 `str`,最后以 `</font>` 结束标记

## 描述

如果你表示的颜色为十六进制 RGB 三原色,则必须使用的格式`rrggbb`。例如,对于橙红色的十六进制 RGB 值是红色=FA,绿色=80,和蓝=72,所以橙红色的 RGB 三原色`"FA8072"`
`fontcolor()` 方法本身只是简单地将字符串部分连接在一起,没有进行任何验证或规范化。然而,为了创建有效的 {{HTMLElement("font")}} 元素,如果你将颜色表示为十六进制 RGB 三元组,你必须使用格式 `rrggbb`。例如,salmon(鲑鱼色)的十六进制 RGB 值为红色 = FA,绿色=80,蓝色=72,因此 salmon 的 RGB 三元组为 `"FA8072"`

## 示例

### 使用 `fontcolor()`
### 使用 fontcolor()

下面的示例使用`fontcolor()`方法来改变字符串的颜色,通过产生一个被 HTML \<font> 标签包裹的字符串
下面的示例使用 `fontcolor()` 方法通过生成一个带有 HTML `<font>` 元素的字符串来改变字符串的颜色

```js
var 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.logworldString.fontcolor('FF00') + ' is red in hexadecimal'
// <font color="FF00">Hello,world </font> is red in hexadecimal
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'
```

{{domxref("HTMLElement.style","element.style")}}对象一起,你可以访问元素的`style`属性,并且更随意的去操纵它,例如:
使用 {{domxref("HTMLElement/style", "element.style")}} 对象,你可以获取元素的 `style` 属性并进行更通用的操作,例如:

```js
document.getElementById("yourElemId").style.color = "red";
```

## Specifications
## 规范

{{Specifications}}

Expand All @@ -58,4 +62,5 @@ document.getElementById("yourElemId").style.color = "red";

## 参见

- [`core-js``String.prototype.fontcolor` 的 polyfill](https://github.com/zloirock/core-js#ecmascript-string-and-regexp)
- {{jsxref("String.prototype.fontsize()")}}

0 comments on commit d0f0417

Please sign in to comment.