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

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

document.links.length ให้จำนวนลิงก์ ( tags) ในเอกสาร

ตัวอย่างที่ 1: 3 ลิงก์

<html> <body> 
<a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br> 
<a href="https://www.tutorialspoint.com/php/index.htm">Php</a><br> 
<a href="https://www.tutorialspoint.com/java8/index.htm">Java</a><br> 
<p id="links"></p> 
<script> 
    document.getElementById("links").innerHTML = "The number of links are: " + document.links.length; 
</script> </body> </html>

ผลลัพธ์

Javascript Php Java The number of links are: 3

ตัวอย่างที่ 2: 2 ลิงก์

<html> <body> 
<a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br> 
<a href="https://www.tutorialspoint.com/bitcoin/index.htm">Bitcoin</a><br> 
<script> 
    document.write("The number of links are: " + document.links.length); 
</script> </body> </html>

ผลลัพธ์

Javascript Bitcoin The number of links are: 2