As a result, it will pop up for the second time in IE6 and IE7 browsers The href value of the A element in result.innerHTML becomes an absolute path.
In fact, our ancestors have encountered these problems long ago (thanks to Uncle Yu for the information):
《getAttribute(”HREF”) is always absolute》
《getAttribute href bug》
The solution has been mentioned in the above article, which is to use the getAttribute('href', 2) method under IE. Microsoft has extended this method with a second parameter that can be set to 0, 1, or 2. If set to 2, the original value of the property is returned.
The script is corrected to:
(function() {
var test = document.getElementById('test');
alert(test.innerHTML);
var result = document.getElementById('result');
result.innerHTML = test. innerHTML;
if(/*@cc_on!@*/0 ) { //if ie
var links1 = test.getElementsByTagName('a');
var links2 = result.getElementsByTagName('a' );
for(var i = 0, len = links1.length; i links2[i].href = links1[i].getAttribute('href', 2); }
}
alert(result.innerHTML);
})();
While searching for this problem, I also searched for one discovered by Hedger Wang Interesting BUG: When resetting a new href attribute value in IE, if the link text contains "http://" or "@", its innerHTML will be displayed incorrectly and will display as the set href attribute.
Solution (sHref is the new value of href to be set):
sHref = 'http://www.hedgerwow.com';
var isMSIE = /*@cc_on!@*/false;
if( isMSIE ){
sHref = ' ' sHref; //add extra space before the new href
};
详细:《Internet Explorer might reset Anchor's innerHTML incorrectly when a new “href” is assigned》