<a href="about.html" id="anc">a link with href="about.html"</a>
var anchor = document.getElementById('anc');
console.log(anchor.href);
console.log(anchor.getAttribute('href'));
以上代码,对于第一个 log,希望得到形如 //liunian.info/about.html
的绝对路径,对于第二个 log,希望得到 about.html 这个相对路径。
但在 ie 下,第二个却也由相对路径变为绝对路径了。
兼容的处理方法是使用第二个参数,这个参数是 IE 私有的,但由于 JavaScript 的语言特性,多参数并不会导致出错,所以,对于 Chrome、FF 等将仍是原来的方法。
anchor.getAttribute('href', 2);
References