--- title: String.prototype.fixed() slug: Web/JavaScript/Reference/Global_Objects/String/fixed tags: - Deprecated - HTML wrapper methods - JavaScript translation_of: Web/JavaScript/Reference/Global_Objects/String/fixed ---
{{JSRef}} {{deprecated_header}}

fixed()方法创建了一个<tt>标签元素将字符串包裹起来,从而让这个字符串里面的内容具有固定间距。

语法

str.fixed()

Return value

返回一个表示 {{HTMLElement("tt")}} HTML 元素的字符串。

描述

fixed() 方法将一个字符串包裹在<tt></tt>标签中,比如: "<tt>str</tt>".

举例

使用fixed()

下面的示例代码使用这个fixed方法来改变字符串的格式:

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

说明

Specification Status Comment
{{SpecName('ES6', '#sec-string.prototype.fixed', 'String.prototype.fixed')}} {{Spec2('ES6')}} Initial definition. Implemented in JavaScript 1.0. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.
{{SpecName('ESDraft', '#sec-string.prototype.fixed', 'String.prototype.fixed')}} {{Spec2('ESDraft')}} Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.

浏览器兼容性

{{CompatibilityTable}}
版本 Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
版本 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

相关链接