close

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

View in English Always switch to English

String.prototype.fixed()

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 值的 fixed() 方法创建一个 <tt> 元素字符串,其中嵌入了调用的字符串(<tt>str</tt>),这会导致该字符串以等宽字体显示。

备注:所有 HTML 包装方法都已被弃用,并且仅为了兼容性而标准化。请使用 DOM API(例如 document.createElement())代替。

语法

js
fixed()

参数

无。

返回值

一个以 <tt> 开始标签开头的字符串,接着是文本 str,然后是 </tt> 结束标签。

示例

使用 fixed()

以下示例使用 fixed 方法来更改字符串的格式:

js
const worldString = "Hello, world";
console.log(worldString.fixed()); // "<tt>Hello, world</tt>"

规范

规范
ECMAScript® 2027 Language Specification
# sec-string.prototype.fixed

浏览器兼容性

参见