Property document.anchors ใช้สำหรับเข้าถึง Collection ของ Anchor Elements (<a>) ที่มี Attribute name ในเอกสาร ไม่รวม Anchor ที่มีแค่ href อย่างเดียว
ตัวอย่างโค้ด
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<p>
<a href="/java/index.htm">Java</a><br>
<a href="/php/index.htm">PHP</a><br>
<a name="Ruby Tutorial">Ruby</a><br>
<a name="WordPress Tutorial">WordPress</a>
</p>
<script>
var num1 = document.links.length; // นับลิงก์ทั้งหมด (มี href)
var num2 = document.anchors.length; // นับ anchor ที่มี name attribute
document.write("How many links? " + num1);
document.write("<br>How many anchors? " + num2);
</script>
</body>
</html>
คำอธิบายผลลัพธ์
document.links.length= 4 (Java, PHP, Ruby, WordPress — ทั้งหมดมี href หรือ name)document.anchors.length= 2 (Ruby, WordPress — มี name attribute เท่านั้น)
ข้อสังเกต: document.anchors ถือว่า Deprecated ใน HTML5 ควรใช้ document.querySelectorAll('a[name]') แทนสำหรับโปรเจกต์สมัยใหม่