Analysis of the Disadvantages of Self-closing Writing Method of XHTML Tags_Experience Exchange

WBOY
Release: 2016-05-16 12:04:44
Original
1351 people have browsed it

如果你熟悉XML相关的开发,可能也就习惯于这种写法,想着XML中任何不含子节点的元素都可以这样写,那么XHTML中没有内容的标签也都可以这样写。XHTML中理论上当然允许任何标签以自关闭的方法来书写,然而浏览器兼容性却带来了新问题,那就是IE无法正确识别某些标签的自关闭写法。

请尝试输入以下XHTML代码并在IE中浏览:<strong><font face="新宋体" size="3"><p>hello <script></script> world</p></font></strong>,你会发现只能看到前面的hello而不见后面的world,这事情让人挺无法解释的吧。可能有不少人都曾经遇到过这个问题,并且花了几个小时在上面都找不到合理的解释。

解释源自另外一段类似的代码:<strong><font face="新宋体" size="3"><p>hello <textarea></textarea> world</p></font></strong>,你在IE中看看其显示效果,能够得到合理的解释了吗?我们能够看到前面的hello正常显示了,而后面的world则显示在textarea里面,这证明IE并没有正确识别textarea标签已经自关闭了,而是当它没有关闭,并将后面的内容识别为textarea内部的内容。

这时候我们就明白前面那段代码为什么看不到后面的world了,因为它被当作script的一部分来识别了。这就说明了,在我们使用XHTML时并不能好像XML那样随意的使用自关闭的写法,只有少数原本不需要关闭的标签可以用自关闭的写法,其他标签即使没有任何内容最好也用成对的关闭写法。

最后需要提醒大家的是,其实弱智的parser不仅仅IE有,很多地方都可能碰到由于parser不严谨而引起的问题,所以我们在书写XHTML的时候还是要迁就一些老HTML继承下来的习惯,不能好像真的XML那样自以为符合标准了就随意写。不信?那么再试一个吧:<strong><font face="新宋体" size="3"><p>hello <br> world</p></font></strong>,留意IE与Opera中的显示效果。

Update: Some readers think that the examples I gave do not comply with the XHTML specification, so please read XHTML specification. Chinese translation of the section Empty Elements As follows: "Empty elements must either have a closing tag or end with />, such as
or


. Please refer to HTML Compatibility Standard for information on ensuring backward compatibility with HTML4 browsers." As you can see,
is also given in the specification. Examples like hr> show that the writing method of
complies with the XHTML specification, but is not compatible with the HTML4 standard. So is XHTML compatible with HTML4? Let’s look at the section Compatibility Issues, Chinese The translation is as follows: "Although XHTML1.0 documents are not required to be compatible with existing browsers, in practice this is not difficult to achieve." Therefore, XHTML does not stipulate that documents must be backward compatible. The examples I gave are all Legal XHTML document fragments, when appearing in complete XHTML, can all pass W3C Markup Validation Service verification.

Update again: Actually, the purpose of writing this article is not to emphasize that only conforming to the XHTML specification is enough, nor is it to emphasize that conforming to XHTML and being compatible with HTML4 is enough, but that more needs should be considered Compatibility situation. For example, your CMS allows users to submit HTML, and the submitted HTML is formatted into XHTML through SgmlReader or other methods, and other XML processing may also be performed. At this time, it is possible to convert the submitted by the user into . In this case, it is not easy to find the problem through tracing and debugging, because XML processing does not violate any specifications, and each step of processing is semantic. In addition, it is best not to write
as
, because there are indeed some mentally retarded parsers that cannot correctly recognize just because there is one missing space.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!