Tautan Dokumen DOM HTML
Contoh
Jumlah tautan dalam dokumen:
let numb = document.links.length;
Dapatkan URL tautan pertama dalam dokumen:
let url = document.links[0].href;
Dapatkan URL tautan pertama dalam dokumen:
let url = document.links.item(0).href;
Lebih banyak contoh di bawah ini.
Definisi dan Penggunaan
Properti links
mengembalikan kumpulan semua tautan dalam dokumen.
Properti forms
mengembalikan HTMLCollection .
Properti links
ini hanya dapat dibaca.
Tautan dalam koleksi mewakili elemen <a> dan <area> dengan atribut href.
Lihat juga:
Koleksi HTML
Sebuah HTMLCollection adalah kumpulan node HTML.
Node dalam koleksi dapat diakses dengan nomor indeks. Indeks dimulai dari 0.
Properti panjang mengembalikan jumlah elemen dalam koleksi.
Sintaksis
document.links
Properti
Property | Description |
length | The number of elements in the collection. |
Metode
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
Nilai Kembali
Jenis | Keterangan |
Obyek | Objek Koleksi HTML . Semua elemen <a> dan <area> dalam dokumen. Elemen diurutkan saat muncul di dokumen. |
Lebih Banyak Contoh
Dapatkan URL elemen dengan id="myLink":
let url = document.links.namedItem("myLink").href;
Tambahkan batas merah ke tautan pertama dalam dokumen:
document.links[0].style.border = "5px solid red";
Ulangi semua tautan dan keluarkan URL (href) masing-masing:
const links = document.links;
let text = "";
for (let i = 0; i < links.length; i++) {
text += links[i].href + "<br>";
}
Dukungan Peramban
document.links
adalah fitur DOM Level 1 (1998).
Ini didukung penuh di semua browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |