จาวาสคริปต์ ได้จัดเตรียม document.links.href เพื่อรับ href คุณลักษณะของลิงค์ที่ต้องการ วิธีนี้ทำงานเหมือนกับวิธีการที่แสดงองค์ประกอบเฉพาะในอาร์เรย์ มาพูดคุยกันสั้นๆ
ตัวอย่าง-1
ในตัวอย่างต่อไปนี้ มีให้สามลิงก์ การใช้ document.links.href ลิงก์ที่ 3 ถูกดำเนินการในเอาต์พุต มันติดตามเช่นเดียวกับอาร์เรย์ ลิงค์แรกจะใช้ดัชนีที่ 0 ลิงค์ที่สองจะใช้ดัชนีที่ 1 และต่อไปเรื่อยๆ
<html>
<body>
<a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br>
<a href="https://www.tutorialspoint.com/java8/index.htm">Java</a><br>
<a href="https://www.tutorialspoint.com/spring/index.htm">Spring</a>
<p id="links"></p>
<script>
document.getElementById("links").innerHTML =
"The href attribute is: " + document.links[2].href;
</script>
</body>
</html> ผลลัพธ์
Javascript Java Spring The href attribute is: https://www.tutorialspoint.com/spring/index.htm
ตัวอย่าง-2
ในตัวอย่างต่อไปนี้ มีการจัดเตรียมลิงก์ไว้สองลิงก์ การใช้ document.links.href ลิงก์ที่ 1 ถูกดำเนินการในเอาต์พุต
<html>
<body>
<a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br>
<a href="https://www.tutorialspoint.com/java8/index.htm">Java</a>;
<p id="links"></p>
<script>
document.getElementById("links").innerHTML =
"The href attribute is: " + document.links[0].href;
</script>
</body>
</html> ผลลัพธ์
Javascript Java The href attribute is: https://www.tutorialspoint.com/javascript/index.htm