Η ιδιότητα links του HTML DOM Document
- previousPage lastModified
- Επόμενη Σελίδα normalize()
- Επιστροφή στο Πάνω επίπεδο HTML DOM Documents
Ορισμός και χρήση
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
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;
Example 2
Get the URL of the first link in the document:
let url = document.links[0].href;
Example 3
Get the URL of the first link in the document:
let url = document.links.item(0).href;
Example 4
Get the URL of the element with id="myLink":
let url = document.links.namedItem("myLink").href;
Example 5
Add a red border to the first link in the document:
document.links[0].style.border = "5px solid red";
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>"; }
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 |
- previousPage lastModified
- Επόμενη Σελίδα normalize()
- Επιστροφή στο Πάνω επίπεδο HTML DOM Documents