When innerHTML is used to obtain HTML code, many double quotation marks of the HTML Tag attribute disappear. It turns out that the attribute value contains spaces before double quotation marks are retained.

Source: Internet
Author: User

When innerHTML is used to obtain HTML code, many double quotation marks of the HTML Tag attribute disappear. It turns out that the attribute value contains spaces before double quotation marks are retained.

The method used in a recent project is strange. The HTML content is displayed with a Label, and then the innerHTML of the Label is repeatedly copied to the TextBox using JS.

However, a problem was found yesterday. When getting the element value, sometimes it is normal, sometimes it is not normal, and then it enters a long DEBUG ..

During the debugging process, monitor the Label and TextBox values and find that the obtained Label value is not changed. when the control is loaded, it is displayed as a SPAN element on the page, when viewing the webpage source code, you can see that the modification has been made, but in the background, no matter how it is obtained, it is the value previously granted during page loading without modification.

However, although the value of TextBox can always get the latest value through the JS assignment, many double quotation marks in the attribute are missing, while the double quotation marks of some attributes are retained, I don't know why.

TextBox is an INPUT element in the webpage. I suspect that TextBox is not suitable for storing HTML code. Then I changed the Label, HiddenField, Div, RenderControl, and other methods, but it was not successful.

Although it is normal to view the innerHTML code of the Div in the webpage, but it cannot be extracted from the server, you can only find another way.

Then I output innerHTML using alert in the most primitive way. The result of alert is no double quotation marks, which is intriguing, does innerHTML automatically remove double quotation marks? How can I display it normally?

After trying for a long time, I wanted to find a method to convert HTML into XHTML, and finally ended in failure ..

I tried to use a lot of different data and try different inputs. I found that an attribute sometimes contains double quotation marks, but sometimes does not. I immediately realized that this is a breakthrough point!

Then, Huang Tianyan was unconcerned. After many attempts and observations, I found the rule of innertHTML. If the attribute value contains spaces, double quotation marks of the attribute will be retained; otherwise, the attribute will not be retained.

With this rule, we can solve the following problems. Why didn't I see this detail at the beginning?

Related Article

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.