When using innerHTML to get HTML code, the double quotation marks of HTML tag properties disappear, so the double quotation marks are preserved when the property value contains a space.

Source: Internet
Author: User

A recent project has been used in a strange way, using a label to display HTML content, and then continue to use JS to copy the label's innerHTML into a textbox.

However, yesterday found a problem, get the element value, sometimes normal, sometimes not normal, and then into the long Debug.

During debugging, the values of the label and TextBox are monitored, and the value obtained to the label is not changed, and the control is loaded with a SPAN element, which can be seen when viewing the source code of the Web page. However, in the background no matter how to get is the page loaded with the value of the previous unmodified.

While the value of the textbox, although through the JS assignment can always get to the latest value, but the inside properties a lot of double quotes are missing, and some properties of the double quotation marks are retained, do not know why.

The textbox is the INPUT element in the Web page, and then I wonder if the textbox is not suitable for storing HTML code, and then changed the label, HiddenField, Div, RenderControl, and so on, but none of them succeeded.

Although it is normal to view the innerHTML code of the div in the Web page, it cannot be extracted from the server, only to find another way.

Then I use the most primitive way, using the alert to innerHTML output, the results found that the result of alert is also no double quotation marks, it is intriguing, is it innerhtml automatically put double quotes to remove it? So how do I get him to show up normally?

Tried for a long time, to find a way to convert HTML into XHTML, and ultimately ended up in failure.

Try to try, with a lot of different data, try different input, found that there is a property sometimes contains double quotes, and sometimes not, I immediately realize that this is a breakthrough!

Then, emperor, after repeated attempts and observations, I found the law of innerthtml, originally the attribute value contains a space, the property of the double quotation marks, no is not preserved .

With this rule, the next question is handled, why didn't I observe the detail in the beginning?

When using innerHTML to get HTML code, the double quotation marks of HTML tag properties disappear, so the double quotation marks are preserved when the property value contains a space.

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.