Bộ sưu tập liên kết trả về danh sách / bộ sưu tập tất cả các liên kết tương ứng với và / hoặc
Cú pháp
Sau đây là cú pháp -
Trả lại liên kết bộ sưu tập
document.links
Thuộc tính
Đây, “liên kết” bộ sưu tập có thể có các thuộc tính và phương thức sau -
| Thuộc tính / Phương pháp | Mô tả |
|---|---|
| chiều dài | Nó trả về số phần tử và |
| namesItem () | Nó trả về phần tử hoặc / và |
Ví dụ
Hãy để chúng tôi xem một ví dụ về độ dài thu thập liên kết tài sản -
<!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="Get Links" onclick="getLinks()">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var extStyle = document.getElementById("extStyle");
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> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Trước khi nhấp vào ‘Nhận liên kết’ nút -
Sau khi nhấp vào ‘Nhận liên kết’ nút -