String.prototype.bold()
已棄用:此特性不再推薦。雖然某些瀏覽器可能仍然支援它,但它可能已經從相關的網路標準中刪除,可能正在刪除過程中,或者可能僅為相容性目的而保留。請避免使用它,如果可能,請更新現有程式碼;請參閱本頁底部的相容性表格以指導您的決策。請注意,此特性可能隨時停止工作。
String 值的 bold() 方法會建立一個在字串外層嵌入 <b> 元素(<b>str</b>)的字串,這會導致該字串顯示為粗體。
注意: 所有 HTML 包裝器方法 都已棄用,僅為相容性而標準化。請使用 DOM API,例如 document.createElement()。
語法
js
bold()
引數
無。
返回值
一個以 <b> 開始標籤、然後是文字 str、最後是 </b> 結束標籤開頭的字串。
示例
使用 bold()
下面的程式碼建立了一個 HTML 字串,然後用它替換文件的正文。
js
const contentString = "Hello, world";
document.body.innerHTML = contentString.bold();
這將建立以下 HTML
html
<b>Hello, world</b>
不要直接使用 bold() 建立 HTML 文字,而應使用 DOM API,例如 document.createElement()。例如:
js
const contentString = "Hello, world";
const elem = document.createElement("b");
elem.innerText = contentString;
document.body.appendChild(elem);
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-string.prototype.bold |
瀏覽器相容性
載入中…