Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีหาจำนวน Anchors ในเอกสารด้วย JavaScript

document.anchors.length ให้จำนวน anchor tags () ในเอกสาร

ไวยากรณ์

length = document.anchors.length;

ตัวอย่างที่ 1: 2 anchors

<html> <body> 
<a name="Tutor">Tutorix</a><br> 
<a name="Totorial">Tutorialspoint</a><br> 
<p id="anchors"></p> 
<script> 
    document.getElementById("anchors").innerHTML = "The number of anchors are: " + document.anchors.length; 
</script> </body> </html>

ผลลัพธ์

Tutorix Tutorialspoint The number of anchors are: 2

ตัวอย่างที่ 2: 3 anchors

<html> <body> 
<a name="Tutor">Tutorix</a><br> 
<a name="Totorial">Tutorialspoint</a><br> 
<a name="Totorials">Tutorialspoint private ltd</a><br> 
<script> 
    document.write(document.anchors.length); 
</script> </body> </html>

ผลลัพธ์

Tutorix Tutorialspoint Tutorialspoint private ltd 3