下面的JS代码通过document.links获取网页中的所有超级链接,从而获得超链接的数量
<!DOCTYPE html> <html> <body> <img src ="https://www.jb51.net/planets.gif" alt="Planets" usemap ="#planetmap"> <map> <area shape="rect" coords="0,0,78,125" href="https://www.jb51.net/sun.htm" alt="Sun"> <area shape="circle" coords="88,60,5" href="https://www.jb51.net/mercury.htm" alt="Mercury"> <area shape="circle" coords="125,60,9" href="https://www.jb51.net/venus.htm" alt="Venus"> </map> <p><a href="https://www.jb51.net/js/">JavaScript Tutorial</a></p> <p>Number of areas/links: <script> document.write(document.links.length); </script></p> </body> </html>
运行结果如下:
Number of areas/links: 4