Η ιδιότητα links του HTML DOM Document

Ορισμός και χρήση

links Η ιδιότητα επιστρέφει τη συλλογή όλων των δεσμών του έγγραφου.

links The property returns HTMLCollection.

links The property is read-only.

The links in the collection represent <a> and <area> elements with the href attribute.

See also:

Το αντικείμενο DOM Anchor του HTML

Το αντικείμενο DOM Area του HTML

HTML <a> Ετικέτα

HTML <area> Ετικέτα

HTML Σύνδεσμοι

HTMLCollection

HTMLCollection It is a similar array collection (list) of HTML elements.

Elements in the collection can be accessed by index (starting from 0).

length The property returns the number of elements in the collection.

Instance

Example 1

Return the number of links in the document:

let numb = document.links.length;

Try it yourself

Example 2

Get the URL of the first link in the document:

let url = document.links[0].href;

Try it yourself

Example 3

Get the URL of the first link in the document:

let url = document.links.item(0).href;

Try it yourself

Example 4

Get the URL of the element with id="myLink":

let url = document.links.namedItem("myLink").href;

Try it yourself

Example 5

Add a red border to the first link in the document:

document.links[0].style.border = "5px solid red";

Try it yourself

Example 6

Traverse all links and output the URL (href) of each link:

const links = document.links;
let text = "";
for (let i = 0; i < links.length; i++) {
  text += links[i].href + "<br>";
}

Try it yourself

Syntax

document.links

Property

Property Description
length The number of elements in the collection.

Method

Method Description
[index]

Returns the element with the specified index (starting from 0).

If the index is out of range, it returns null.

item(index)

Returns the element with the specified index (starting from 0).

If the index is out of range, it returns null.

namedItem(id)

Returns the element with the specified id element.

if id If it does not exist, it returns null.

Return value

Type Description
Object

HTMLCollection object.

All <a> and <area> elements in the document.

is sorted in the order they appear in the source code.

Browser support

document.links It is a DOM Level 1 (1998) feature.

All browsers support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
supported 9-11 supported supported supported supported