Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Bộ sưu tập links trong HTML DOM

Bộ sưu tập links trả về danh sách/tập hợp của tất cả các liên kết tương ứng với các phần tử <a> và/hoặc <area>.

Cú pháp

Trả về bộ sưu tập links:

document.links

Các thuộc tính

Bộ sưu tập links có thể có các thuộc tính và phương thức sau:

Thuộc tính/Phương thứcMô tả
lengthTrả về số lượng phần tử <a> và <area>
namedItem()Trả về phần tử <a> hoặc/ và <area> với id được chỉ định

Ví dụ

Ví dụ minh họa thuộc tính links collection length:

<!DOCTYPE html> <html> <head> <title>Links Collection length</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } area { border:1px solid black; } </style> </head> <body> <form> <fieldset> <legend>Links-Collection-length</legend> <a href="https://www.google.com">Google</a> <a href="https://www.bing.com">Bing</a> <input type="button" value="Lấy liên kết" onclick="getLinks()"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); function getLinks(){ var links = document.links.length; divDisplay.textContent = 'Total Links: '+links; for (var i = 0; i < links; i++) { divDisplay.textContent += '--> '+document.links[i]+' '; } } </script> </body> </html>

Kết quả

Trước khi nhấn nút 'Lấy liên kết':

Bộ sưu tập links trong HTML DOM

Sau khi nhấn nút 'Lấy liên kết':

Bộ sưu tập links trong HTML DOM