HTML DOM anchorsCollection

Document 对象 参考手册Document Object

Definition and Usage

The anchors collection returns an array of all hyperlinks on the current page.

Syntax

document.anchors[].property


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the anchors collection.


Examples

Example 1

Return the number of links in the document:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example (example.com)</title>
</head>
<body>

<a name="html">HTML Tutorial</a><br>
<a name="css">CSS Tutorial</a><br>
<a name="xml">XML Tutorial</a><br>
<a href="../js/index.html">JavaScript Tutorial</a>

<p>Number of anchors:
<script>
document.write(document.anchors.length);
</script></p>

</body>
</html>

The output of the above example:

Number of anchors: 3

Try it Yourself »

Example 2

Return the anchor text of the first hyperlink in the document:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example (example.com)</title>
</head>
<body>

<a name="html">HTML Tutorial</a><br>
<a name="css">CSS Tutorial</a><br>
<a name="xml">XML Tutorial</a>

<p>The first anchor in the document:
<script>
document.write(document.anchors[0].innerHTML);
</script>
</p>

</body>
</html>

The output of the above example:

The first anchor in the document: HTML Tutorial

Try it Yourself »


Document 对象 参考手册Document Object Other Extensions