Brief description <br />In IE6 and 7, in a general a tag (an a tag written in HTML and inserted into the page through DOM operations), if the value of href is a relative path, the original value of href will not be obtained directly through a.getAttribute("href"), but the original value can be obtained through a.getAttribute("href",2); however, if this a tag is inserted through innerHTML, the original href value cannot be obtained even through a.getAttribute("href",2). It is estimated that when innerHTML='<a href="/haha">test</a>', IE6 and 7 will make compatibility processing for it and add some things. At this time, when viewed through outerHTML, the href of a is already the complete address -_-! It is said that a similar situation will occur in the src of img. Copy code The code is as follows:<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> <a id="a" href="" onclick=".getAttribute(\"href\",2)">test</a> </body> </html> When you click on it in IE6 or 7, a big “/haha” will pop up! ! ! I started to suspect that something else was at work. At this time, I thought about how my a tag came from (string-based js template, innerHTML)... So, I simulated it through the following code: Copy code The code is as follows:<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> <div id="test"></div> <script> document.getElementById("test").innerHTML = '<a id="a" href="/haha" onclick="alert(this.getAttribute(\'href\',2));return false;">test</a>'; </script> </body> </html> Tested again...sister's...the problem reappeared! In actual testing, after generating a, setAttribute("href","/haha",2) on a again, and then getAttribute("href",2) can get back "/haha". But if you want to do this kind of thing in a template, it seems too disgusting, so give up decisively! It is better to listen to Frank's advice... Simply add an attribute to identify the two link elements, and don't get stuck in a rut. Drill again-_-!: Copy code The code is as follows:<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> <div id="test"></div> <script> var test = document.getElementById("test"); var a = document.createElement("a"); var txt = document.createTextNode("test"); a.href="/haha"; a.onclick=function() { alert(this.getAttribute('href',2));//"/haha" return false; }; a.appendChild(txt); test.appendChild(a); </script> </body> </html> Finally, I suspect that when inserting nodes through innerHTML, IE6 and 7 will do some "error tolerance" processing that it thinks is correct... and then I was wrong... |
<<: Introduction to new features of ECMAscript
>>: A brief analysis of the differences between undo, redo and binlog in MySQL
mysql id starts from 1 and increases automaticall...
In our daily business, form validation is a very ...
background In the group, some students will ask r...
In the horizontal direction, you can set the row ...
Preface In many MySQL test scenarios, some test d...
Table of contents 1. Merge interface 1.1 Non-func...
The installation and configuration method of MySQ...
The <input> tag The <input> tag is us...
There are two types of hard disks in Linux: mount...
Table of contents Basic Edition Step 1: Configure...
Preface The project requirement is to determine w...
Installation Steps 1. Install Redis Download the ...
step: 1. Create a new docker-compose.yml file in ...
The bash history command in Linux system helps to...
The company's service uses docker, and the di...