--- title: String.prototype.link() slug: Web/JavaScript/Reference/Global_Objects/String/link translation_of: Web/JavaScript/Reference/Global_Objects/String/link --- <div>{{JSRef}} {{deprecated_header}}</div> <p><strong><code>link()</code></strong> 方法创建一个 HTML 元素 {{HTMLElement("a")}} ,用该字符串作为超链接的显示文本,参数作为指向另一个 URL 的超链接。</p> <h2 id="语法">语法</h2> <pre class="syntaxbox"><code><var>str</var>.link(<var>url</var>)</code></pre> <h3 id="参数">参数</h3> <dl> <dt><code>url</code></dt> <dd>任何能够指定 <code>a</code> 标签的 <code>href</code> 属性的字符串;它应当是有效的 URL(相对或绝对),任何 <code>&</code> 字符将会被转义为 <code>&amp;</code>,任何 <code>"</code> 字符将会被转义为 <code>&quot;</code>。</dd> </dl> <h3 id="返回值">返回值</h3> <p>一个带有一个 HTML 元素 {{HTMLElement("a")}} 的字符串。</p> <h2 id="描述">描述</h2> <p>使用 <code>link</code> 方法创建一个超链接 HTML 片段。返回的字符串可以通过 {{ Domxref("document.write") }} 或 {{ Domxref("element.innerHTML") }} 方法添加到文档中。</p> <p>使用 <code>link</code> 方法创建的链接将会成为 document.links 数组中的元素。查看 {{ Domxref("document.links") }}。</p> <h2 id="Examples" name="Examples">示例</h2> <h3 id="例子:使用_link">例子:使用 <code>link</code></h3> <p>下例显示一个单词 "MDN" 作为超链接,指向 Mozilla Developer Network。</p> <pre class="brush:js">var hotText = 'MDN'; var URL = 'https://developer.mozilla.org/'; document.write('Click to return to ' + hotText.link(URL)); // Click to return to <a href="https://developer.mozilla.org/">MDN</a> </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('ES6', '#sec-string.prototype.link', 'String.prototype.link')}}</td> <td>{{Spec2('ES6')}}</td> <td>Initial definition. Implemented in JavaScript 1.0. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-string.prototype.link', 'String.prototype.link')}}</td> <td>{{Spec2('ESDraft')}}</td> <td>Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers.</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("javascript.builtins.String.link")}}</p> <h2 id="参见">参见</h2> <ul> <li>{{jsxref("String.prototype.anchor()")}}</li> </ul>