Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách tìm số lượng liên kết trong tài liệu JavaScript


JavaScript DOM cung cấp nhiều thuộc tính lấy chi tiết tài liệu. Có document.links.length lấy số lượng liên kết trong tài liệu. Không ảnh hưởng chức năng khác.

Ví dụ 1

Ba liên kết trong mã. Dùng document.links.length hiển thị số lượng.

<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">Php</a><br> <p id="links"></p> <script> document.getElementById("links").innerHTML = "The number of links are: " + document.links.length; </script> </body> </html>

Kết quả

Javascript Php Java The number of links are: 3


Ví dụ 2

Hai liên kết. Dùng document.links.length hiển thị số lượng.

<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>

Kết quả

Javascript Bitcoin The number of links are: 2