このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

String.prototype.sub()

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.

sub()String 値のメソッドで、文字列を <sub> 要素に埋め込んだ文字列 (<sub>str</sub>) を生成し、文字列が下付き文字として表示されるようにします。

メモ: HTML ラッパーメソッドはすべて非推奨となっており、互換性目的のみで標準化されています。代わりに DOM APIdocument.createElement() などを使用してください。

構文

js
sub()

引数

なし。

返値

開始タグ <sub> で始まり、テキスト str が来て、終了タグ </sub> が来る文字列です。

sub() の使用

以下のコードは、HTML 文字列を生成し、その文字列で文書の本体を置き換えます。

js
const contentString = "Hello, world";

document.body.innerHTML = contentString.sub();

これにより、次の HTML が生成されます。

html
<sub>Hello, world</sub>

sub() を使用して HTML テキストを直接作成する代わりに、document.createElement() などの DOM API を使用しましょう。例を示します。

js
const contentString = "Hello, world";
const elem = document.createElement("sub");
elem.innerText = contentString;
document.body.appendChild(elem);

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-string.prototype.sub

ブラウザーの互換性

関連情報