概述
link() 方法创建一个 <a> HTML 元素,用该字符串作为超链接的显示文本,参数作为指向另一个 URL 的超链接。
语法
str.link(url)
参数
url- 任何能够指定
a标签的href属性的字符串;它应当是有效的 URL(相对或绝对),任何&字符将会被转义为&,任何"字符将会被转义为"。
描述
使用 link 方法创建一个超链接 HTML 片段。返回的字符串可以通过 document.write 或 element.innerHTML 方法添加到文档中。
使用 link 方法创建的链接将会成为 document.links 数组中的元素。查看 document.links。
示例
例子:使用 link
下例显示一个单词 "MDN" 作为超链接,指向 Mozilla Developer Network。
var hotText = "MDN";
var URL = "https://developer.mozilla.org/";
document.write("Click to return to " + hotText.link(URL));
该例输出下面的 HTML
Click to return to <a href="https://developer.mozilla.org/">MDN</a>
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) String.prototype.link |
Standard | Initial definition. Implemented in JavaScript 1.0. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers. |
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | 1.0 (1.7 or earlier) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 1.0 (1.0) | (Yes) | (Yes) | (Yes) |
Gecko-specific notes
- Starting Gecko 17.0 (Firefox 17 / Thunderbird 17 / SeaMonkey 2.14) the " (quotation mark) is now automatically replaced by its HTML reference character
"in theurlparameter.