Home >Web Front-end >JS Tutorial >GetAttribute in IE6/7 obtains the href/src attribute (the relative path 0 value is different from other browsers_javascript skills

GetAttribute in IE6/7 obtains the href/src attribute (the relative path 0 value is different from other browsers_javascript skills

PHP中文网
PHP中文网Original
2016-05-16 18:02:501324browse

The solution for getting the href/src attribute (relative path 0 value) in IE6/7 is different from other browsers

The test code is as follows:

 
<a href="/abc/index.html">home</a> 
<img src="http://files.jb51.net/upload/201108/20110828174815833.gif"> 
<script> 
var link = document.getElementsByTagName(&#39;a&#39;)[0]; 
var img = document.getElementsByTagName(&#39;img&#39;)[0]; 
alert(link.getAttribute(&#39;href&#39;)); 
alert(img.getAttribute(&#39;src&#39;)) 
</script>

There are elements a and img (Standard document mode), the relative path is set as follows:

IE6/7: Return the full path


IE8/9/10/Firefox/Safari/Chrome/Opera: Return relative path

If you want to be consistent with other browsers in IE6/7, you can set the second parameter of getAttribute to 2. The standard getAttribute method does not define a second parameter. The magic of IE is as follows: MSDN's description of the setAttribute parameter

 
<a href="/abc/index.html">home</a> 
<img src="http://files.jb51.net/upload/201108/20110828174815833.gif"> 
<script> 
var link = document.getElementsByTagName(&#39;a&#39;)[0]; 
var img = document.getElementsByTagName(&#39;img&#39;)[0]; 
alert(link.getAttribute(&#39;href&#39;, 2)); // 注意第二个参数 
alert(img.getAttribute(&#39;src&#39;, 2)); // // 注意第二个参数 
</script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn