JavaScript cung cấp document.links.href lấy thuộc tính href của liên kết cần thiết. Hoạt động như truy xuất phần tử trong mảng. Thảo luận sơ lược.
Ví dụ 1
Ba liên kết cung cấp. Dùng document.links.href lấy liên kết thứ 3 (index 2). Link đầu index 0, thứ hai index 1, v.v.
<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>
Kết quả
Javascript Java Spring The href attribute is: https://www.tutorialspoint.com/spring/index.htm
Ví dụ 2
Hai liên kết. Lấy liên kết đầu tiên (index 0).
<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>
Kết quả
Javascript Java The href attribute is: https://www.tutorialspoint.com/javascript/index.htm