JS案例:获取当前页面上所有的超链接

来自CloudWiki
跳转至: 导航搜索
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>访问指定元素</title>
</head>
<body>
<p><span>人邮教育</span>的主页地址是
<a id="rj" href="http://www.ryjiaoyu.com">http://www.rjjiaoyu.com</a>。</p>
<p><span>人民邮电出版社</span>的主页地址是
<a id="ptpress" href="http://www.ptpress.com">http://www.ptpress.com.cn</a>。</p>
<pre>
<script type="text/javascript">
  	document.writeln("本页面有以下超链接地址:");
 	var arr=document.getElementsByTagName("a");
 	for(var i=0;i<arr.length;i++) {
    //遍历所有超链接
		document.writeln(arr[i].href);
	    }
    document.writeln("其中,");
    document.write("   人邮教育的网址是 ");
 	document.writeln(document.links[0].href);
	document.write("   人民邮电的网址是 ");
	document.writeln(document.links[1].href);
</script>
</pre></body></html>