fixed() 方法會讓字串看起來像是包在 <tt>(teletype,等寬字體)標籤中一樣,以固定間距的字體顯示。它是 JavaScript 字串的一組早期 HTML 包装方法之一。
string.fixed()
此方法沒有參數(標示為 NA)。
回傳帶有 <tt> 標籤的字串。
<html>
<head>
<title>JavaScript String fixed() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String("Hello world");
alert(str.fixed());
</script>
</body>
</html>
執行後會輸出:
<tt>Hello world</tt>
fixed() 產生的 <tt> 標籤在現代 HTML 中同樣已屬過時。若需要以等寬字體顯示內容(例如程式碼片段),建議改用 <code> 或 <pre> 標籤搭配 CSS 的 font-family: monospace,語意更明確、也更符合目前的網頁標準。