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

View in English Always switch to English

String.prototype.link()

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.

link()String 値のメソッドで、この文字列を <a> 要素に埋め込み (<a href="...">str</a>)、他の URL へのハイパーテキストリンクとして使用される文字列を生成します。

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

構文

js
link(url)

引数

url

<a> 要素の href 属性を指定する任意の文字列です。これは妥当な(相対または絶対)URL で、すべての & の文字を &amp; でエスケープしたものです。

返値

開始タグ <a href="url">url の中の二重引用符は &quot; に置き換えられます)、str のテキストが来て、終了タグ </a> が来る文字列です。

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

js
const contentString = "MDN Web Docs";

document.body.innerHTML = contentString.link("https://developer.mozilla.org/");

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

html
<a href="https://developer.mozilla.org/">MDN Web Docs</a>

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

js
const contentString = "MDN Web Docs";
const elem = document.createElement("a");
elem.href = "https://developer.mozilla.org/";
elem.innerText = contentString;
document.body.appendChild(elem);

仕様書

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

ブラウザーの互換性

関連情報