JS innerHTML Double Quotes

Source: Internet
Author: User

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:

  1. InnerHTML removes attribute quotes in Internet Explorer
  2. Where are the double quotation marks in innerHTML?



Reference: http://liupeng.us/how-to-fix-js-innerhtml-double-quotes/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.