您好,欢迎访问一九零五行业门户网

IE6/7中getAttribute获取href/src 属性(相对路径0值与其它浏览器不同_javascript技巧

ie6/7中getattribute获取href/src 属性(相对路径0值与其它浏览器不同的解决方法
测试代码如下: 
<a href="/abc/index.html">home</a> <img src="http://files.jb51.net/upload/201108/20110828174815833.gif"> <script> var link = document.getelementsbytagname('a')[0]; var img = document.getelementsbytagname('img')[0]; alert(link.getattribute('href')); alert(img.getattribute('src')) </script>
有元素a和img(标准文档模式),设置了相对路径。各浏览器效果如下
ie6/7:返回完整路径
ie8/9/10/firefox/safari/chrome/opera:返回相对路径
ie6/7中想要与其它浏览器保持一致的话,可以给getattribute的第二个参数设为2。
<a href="/abc/index.html">home</a> <img src="http://files.jb51.net/upload/201108/20110828174815833.gif"> <script> var link = document.getelementsbytagname('a')[0]; var img = document.getelementsbytagname('img')[0]; alert(link.getattribute('href', 2)); // 注意第二个参数 alert(img.getattribute('src', 2)); // // 注意第二个参数 </script>
标准的getattribute方法是没有定义第二个参数的,神奇的ie啊。以下是msdn对setattribute参数的描述
其它类似信息

推荐信息