String.prototype.fixed()

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 值的 fixed() 方法创建一个字符串,将该字符串嵌入到 <tt> 元素 (<tt>str</tt>) 中,从而导致该字符串以固定宽度字体显示。

¥The fixed() method of String values creates a string that embeds this string in a <tt> element (<tt>str</tt>), which causes this string to be displayed in a fixed-width font.

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

¥Note: All HTML wrapper methods are deprecated and only standardized for compatibility purposes. For the case of fixed(), the <tt> element itself has been removed from the HTML specification and shouldn't be used anymore. Web developers should use CSS properties instead.

语法

¥Syntax

js
fixed()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

<tt> 开始标记开始的字符串,然后是文本 str,最后是 </tt> 结束标记。

¥A string beginning with a <tt> start tag, then the text str, and then a </tt> end tag.

示例

¥Examples

使用固定()

¥Using fixed()

下面的代码创建一个 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.fixed();

这将创建以下 HTML:

¥This will create the following HTML:

html
<tt>Hello, world</tt>

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

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

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

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

js
document.getElementById("yourElemId").style.fontFamily = "monospace";

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看