HTML DOM links 集合
Document 對象定義和用法
links 集合返回當(dāng)期文檔所有鏈接的數(shù)組。
提示: links 集合計(jì)算 <a href=""> 標(biāo)簽和 <area> 標(biāo)簽。
語法
document.links[].property
瀏覽器支持
所有主要瀏覽器都支持 links 集合
實(shí)例
實(shí)例 1
返回文檔的鏈接數(shù):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p><a href="/javascript/">JavaScript 教程</a></p>
<p>鏈接/地址數(shù)目:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p><a href="/javascript/">JavaScript 教程</a></p>
<p>鏈接/地址數(shù)目:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
以上實(shí)例輸出結(jié)果:
鏈接/地址數(shù)目: 4
嘗試一下 ?
實(shí)例 2
返回文檔的第一個(gè)鏈接:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p><a id="javascript" href="/javascript/">JavaScript 教程</a></p>
<p>第一個(gè)鏈接/地址的ID:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p><a id="javascript" href="/javascript/">JavaScript 教程</a></p>
<p>第一個(gè)鏈接/地址的ID:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
以上實(shí)例輸出結(jié)果:
第一個(gè)鏈接/地址的ID:sun
嘗試一下 ?
Document 對象
更多建議: