Get a dom node through innerhtml of js. If the internal html value does not contain spaces, the labels will be larger in ie versions or earlier, and double quotation marks will be lost. If the attribute value contains spaces, the tag is capitalized and the quotation marks are output normally.
When the attribute value has no space:
FF3.5.7 (gecko/20091221) <div id = "div2"> </div>
IE6 pop-up: <DIV id = div2> </DIV>
IE7 pop-up: <DIV id = div2> </DIV>
IE8 pop-up: <DIV id = div2> </DIV>
When spaces exist:
FF3.5.7 (gecko/20091221) <div id = "div2"> </div>
IE6 pop-up: <DIV id = "div2"> </DIV>
IE7 pop-up: <DIV id = "div2"> </DIV>
IE8 pop-up: <DIV id = "div2"> </DIV>
What is the cause of this BUG?
When you get the innerHTML of a DOM node in IE, if there are no spaces in an attribute value, IE will remove the quotes around it. IE's innerHTML doesn' t remove quotes from non standard attributes.
Even in jQuery, The jquery team's technical staff did not solve this problem.
Provide the following solutions:
function ieInnerHTML(obj) { var zz = obj.innerHTML, z = zz.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g); if (z){ for (var i=0;i<z.length;i++){ var y, zSaved = z[i]; z[i] = z[i].replace(/(<?\w+)|(<\/?\w+)\s/, function(a){return a.toLowerCase();}); y = z[i].match(/\=\w+[?\s+|?>]/g); if (y){ for (var j=0;j<y.length;j++){ z[i] = z[i].replace(y[j],y[j] .replace(/\=(\w+)([?\s+|?>])/g,'="$1"$2')); } } zz = zz.replace(zSaved,z[i]); } } return zz; }
Call a function in the appropriate placeIeInnerHTML
If (/* @ cc_on! @ */0 ){
Inner_str = ieInnerHTML (obj );
} Else {
Inner_str = obj. innerHTML;
}
Principle: JS processing is performed on the IE version, and non-standard elements are not allowed in the corresponding element labels.
References:
- InnerHTML removes attribute quotes in Internet Explorer
- Where are the double quotation marks in innerHTML?
Reference: http://liupeng.us/how-to-fix-js-innerhtml-double-quotes/