Analysis on disadvantages of self-closing Syntax of XHTML labels

Source: Internet
Author: User
We all know that the img mark in XHTML should be written as follows: & lt; imgalt & quot; src & quot; & gt ;, this writing method is called self-closing, which is completely legal in XML. If you are familiar with XML-related development, you may be used to this writing method, thinking that any element in XML that does not contain child nodes can be written in this way, the labels that do not have any content in XHTML can also be written in this way. In XHTML theory, of course, any label can be written in self-closing mode. However, browser compatibility brings about a new problem, that is, IE cannot correctly identify the self-closing method of some labels.

Enter the following XHTML code and browse it in IE:

hello world

You will find that you can only see the preceding hello but not the following world, which is quite undefined. Many people may have encountered this problem and could not find a reasonable explanation after several hours.

The explanation is derived from another similar code:

HelloWorld </p> </STRONG> </CODE>: Can you reasonably explain the Display Effect in IE? We can see that the preceding hello is displayed normally, while the following world is displayed in textarea, which proves that IE does not correctly identify that the textarea tag has been disabled, but when it is not disabled, the following content is recognized as the content inside textarea. </P> <P> at this time, we can see why the previous Code cannot see the world, because it is recognized as part of the script. This shows that when we use XHTML, we cannot use the self-closing method as if we were using XML. Only a few labels that do not need to be closed can be self-disabled, other labels should be closed in pairs even if there is no content. </P> <P> in the end, we need to remind you that, in fact, mentally retarded parser is not only available in IE, but may encounter problems caused by not rigorous parser in many places, therefore, when writing XHTML, we still need to accommodate some old HTML inherited habits. It cannot be written as if the XML was as self-satisfied as the standard. Believe it? Try again: <CODE> <STRONG> <p> hello <br> <br/> world </p> </STRONG> </CODE>, pay attention to the display effects in IE and Opera. </P> <STRONG> Update: </STRONG> some readers think that the example does not comply with the XHTML specification. Read the XHTML specification first. The English translation of the Empty Elements section is as follows: "An Empty element must either have an ending mark or end with/>, for example, <br/> or In this case, it is not easy to identify the problem through tracking and debugging, Because XML processing does not violate any specifications, and each step is semantic-compliant. In addition, it is better not
Write
Because it is true that some mentally retarded parser cannot be correctly identified simply because a space is missing.

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.