Home > Web Front-end > JS Tutorial > body text

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

PHP中文网
Release: 2016-05-16 18:02:50
Original
1299 people have browsed it

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>
Copy after login

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>
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template