String.prototype.fontsize()

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 值的 fontsize() 方法创建一个字符串,将该字符串嵌入到 <font> 元素 (<font size="...">str</font>) 中,从而导致该字符串以指定的字体大小显示。

¥The fontsize() method of String values creates a string that embeds this string in a <font> element (<font size="...">str</font>), which causes this string to be displayed in the specified font size.

注意:所有 HTML 封装方法 均已弃用,仅出于兼容性目的而进行标准化。对于 fontsize() 的情况,<font> 元素本身已从 HTML 规范中删除,不应再使用。Web 开发者应该使用 CSS 属性。

¥Note: All HTML wrapper methods are deprecated and only standardized for compatibility purposes. For the case of fontsize(), 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

js
fontsize(size)

参数

¥Parameters

size

1 到 7 之间的整数,或表示 1 到 7 之间有符号整数的字符串。

返回值

¥Return value

<font size="size"> 开始标记(size 中的双引号替换为 &quot;)开头的字符串,然后是文本 str,然后是 </font> 结束标记。

¥A string beginning with a <font size="size"> start tag (double quotes in size are replaced with &quot;), then the text str, and then a </font> end tag.

描述

¥Description

fontsize() 方法本身只是将字符串部分连接在一起,而不进行任何验证或规范化。但是,要创建有效的 <font> 元素,当你将大小指定为整数时,请将 str 的字体大小设置为 7 个定义的大小之一。你可以将 size 指定为字符串,例如 "-2""+3",以调整 str 相对于默认值 3 的字体大小。

¥The fontsize() method itself simply joins the string parts together without any validation or normalization. However, to create valid <font> elements, When you specify size as an integer, you set the font size of str to one of the 7 defined sizes. You can specify size as a string such as "-2" or "+3" to adjust the font size of str relative to 3, the default value.

示例

¥Examples

使用字体大小()

¥Using fontsize()

下面的代码创建一个 HTML 字符串,然后用它替换文档的正文:

¥The code below creates an HTML string and then replaces the document's body with it:

js
const contentString = "Hello, world";

document.body.innerHTML = contentString.fontsize(7);

这将创建以下 HTML:

¥This will create the following HTML:

html
<font size="7">Hello, world</font>

警告:此标记无效,因为 font 不再是有效元素。

¥Warning: This markup is invalid, because font is no longer a valid element.

你应该使用 CSS 来操作字体,而不是使用 fontsize() 直接创建 HTML 文本。例如,你可以通过 element.style 属性操作 font-size

¥Instead of using fontsize() and creating HTML text directly, you should use CSS to manipulate fonts. For example, you can manipulate font-size through the element.style attribute:

js
document.getElementById("yourElemId").style.fontSize = "7pt";

规范

Specification
ECMAScript Language Specification
# sec-string.prototype.fontsize

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看