--- title: String.prototype.anchor() slug: Web/JavaScript/Reference/Global_Objects/String/anchor tags: - HTML - JavaScript - Method - Prototype - String translation_of: Web/JavaScript/Reference/Global_Objects/String/anchor ---
警告:该特性已经从 Web 标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性。
anchor()
方法创建一个 {{HTMLElement("a")}} HTML 锚元素,被用作超文本靶标(hypertext target)。
str.anchor(name)
name
name
属性。包含 {{HTMLElement("a")}} HTML元素的一个字符串。
使用 anchor
方法能够以编程方式在一个文档中创建和展现一个锚链接。
语法上来讲,字符串表示你想让用户看到的文本。name
字符串参数表示 {{HTMLElement("a")}} 元素的 name
属性。
使用 anchor 方法创建的锚点(anchors)将会成为 {{domxref("document.anchors")}} 数组的元素。
anchor
var myString = "Table of Contents"; document.body.innerHTML = myString.anchor("contents_anchor");
将会输出下面的 HTML:
<a name="contents_anchor">Table of Contents</a>
规范 | 状态 | 注释 |
---|---|---|
{{SpecName('ES6', '#sec-string.prototype.anchor', 'String.prototype.anchor')}} | {{Spec2('ES6')}} | Initial definition. Implemented in JavaScript 1.0. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers. |