如何用Javascript获取超链接的链接地址?

来源:百度知道 编辑:UC知道 时间:2024/05/15 07:40:50
比如说 <a href="http://zhidao.baidu.com">知道</a>

我想得到这个http://zhidao.baidu.com这个值,请问用Javascript如何才能实现?

另外说明下,http://zhidao.baidu.com这个值不固定,可能是http://www.baidu.com

可以用getAttribute()方法获取。
注:总结了一些getAttribute(),setAttribute()在不同浏览器下兼容性以及如何解决这些问题
body里面有这些内容:
<div id="idHeader" class="class-header" title="kingwell" status="1"></div>
<label id="forUserName" for="userName" title="kingwell" status="1"></label>
下面是script的测试:
var el = document.getElementById("idHeader");
alert(el.getAttribute("id"));
alert(el.id);
IE Firfox->idHeader

alert(el.getAttribute("class"));
//IE6,IE7 -> null IE8,IE9,Firefox ->class-header

alert(el.class);
//IE6,IE7,IE8->报错 IE9,Firefox->undefined
alert(el.getAttribute("className"));
//IE6,IE7->class-header ; IE8,IE9,Firefox -> undefined

alert(el.className);
//All -> class-header

var elfor = document.getElementById(&quo