Thẻ neo (anchor tags) của JavaScript tuân theo cấu trúc giống mảng (array-like). Để hiển thị neo cụ thể, dùng document.anchors.innerHTML. Hoạt động như phương thức mảng lấy phần tử cụ thể.
Cú pháp
element = document.anchors[i].innerHTML;
Ví dụ 1
Hiển thị neo đầu tiên trong 2 neo.
<html> <body> <a name="Tutor">Tutorix</a><br> <a name="Totorial">Tutorialspoint</a><br> <p id="anchors"></p> <script> document.getElementById("anchors").innerHTML = "The first anchor element is: " + document.anchors[0].innerHTML; </script> </body> </html>
Kết quả
Tutorix
Ví dụ 2
3 neo, lấy neo thứ 3 (index 2).
<html> <body>
<a name="Tutor">Tutorix</a><br>
<a name="Totorial">Tutorialspoint</a><br>
<a name="Totorials">Tutorialspoint private ltd</a><br>
<p id="anchors"></p>
<script>
document.getElementById("anchors").innerHTML = "The third element is:" + document.anchors[2].length;
</script> </body> </html>
Kết quả
Tutorix Tutorialspoint Tutorialspoint private ltd The third element is:Tutorialspoint private ltd