Pay attention to the self-closing of labels when making Web pages with XHTML

Source: Internet
Author: User
Tags empty end
xhtml| Web page

We all know that the IMG tags in XHTML should be written like this: this kind of writing is called Self shutdown, which is perfectly legal in XML. If you are familiar with XML-related development, you may also be accustomed to writing, thinking that any element that does not have a child node in XML can be written in this way, so that tags with no content in XHTML can be written as well. XHTML, of course, allows any tag to be written in a closed way, but browser compatibility poses a new problem, IE does not correctly recognize the custom of some tags.

Please try entering the following XHTML code and browsing in IE: <p>hello <script type= "Text/javascript"/> World</p> You will find that you can only see the front of hello but not the back of the world, this thing is very inexplicable. There may be a lot of people who have had this problem and spent a few hours on it and couldn't find a reasonable explanation.

The explanation comes from another piece of code similar to this: <p>hello <textarea/> world</p> Do you see the display effect in IE, can you get a reasonable explanation? We can see the previous Hello normal display, and the world behind it is shown in textarea, which proves that IE did not correctly identify the TEXTAREA tag has been closed, but when it is not closed, and the following content is recognized as textarea internal content.

That's when we know why the previous code doesn't see the world in the back because it's being identified as part of the script. This means that when we use XHTML, we do not use the same way as XML, and only a handful of tags that do not need to be closed can be written in a closed form, and other tags, even if there is no content, are best used in pairs.

Finally need to remind everyone is that, in fact, the parser of the mentally retarded not only ie, many places may encounter due to the parser of the problem, so we are writing XHTML or to accommodate some old HTML inherited habits, You can't just write as if you think you can do it as if you were really XML. Don't believe it? Then try one more: <p>hello <br></br> World</p> Pay attention to the display effect of IE and opera.

Update: Some readers think that my example does not conform to the XHTML specification, so please read the XHTML specification first. The Chinese translation of the EMPTY elements section reads as follows: "An empty element must either have an end tag or end with a/>, such as <br/> or

Update again: In fact, the purpose of my writing this article is not to emphasize conforming to the XHTML specification alone, nor to emphasize that conforming to XHTML and compatibility with HTML4 is enough, but should consider more situations that need to be compatible. For example, your CMS allows users to submit HTML, submitted HTML through Sgmlreader or other methods formatted as XHTML, and perhaps other XML processing, it is possible to submit the user's <textarea></ Textarea> is converted to <textarea/>, in which case you need to trace debugging to find out the problem is not easy, because the XML processing does not violate any specification, each step of the processing is consistent with semantics. In addition, it is best not to write the <br/> as <br/>, because some of the parser of the mentally retarded can not be properly 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.