本文介绍了在一个HTML文档的多个文档类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个HTML文档有两个文档类型,如何将文档类型影响页面的渲染和文档类型将浏览器挑?是具有两个(或更多)的文档类型在一个文档中有效或混乱?

例如:

 <!DOCTYPE HTML>
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.0过渡// ENGT&;
< HTML和GT;< / HTML>


解决方案

只有一个单一的文档类型声明是允许的。在此之前,而不是直接从HTML规范以及HTML5的草案,它也可以使用查了。

因此​​,不存在什么规范应的发生。自然的期望是,由于浏览器的浏览器模式决定(当处理DOCTYPE声明只能在DOCTYPE嗅探的与标准模式),只有第一个doctype声明生效,其他被忽略。

这可以例如测试如下(使用HTML 3.2的doctype,这将触发所有的doctype嗅探浏览器Quirks模式):

 <!DOCTYPE HTML>
< D​​OCTYPE HTML PUBLIC - // W3C // DTD HTML 3.2的最终// EN&GT!;
< HTML和GT;
<标题>测试重复DOCTYPE效应LT; /标题>
<脚本>
文件撰写(document.compatMode);
< / SCRIPT>
< / HTML>

这显示CSS1Compat(=标准模式),而交换的DOCTYPE声明会导致BackCompat(=怪癖模式)。

If a HTML document has two doctypes, how will the doctypes affect the rendering of the page and which doctype would the browser pick? Is having two (or more) doctypes in a single document valid or confusing?

Example:

<!DOCTYPE html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>

</html>
解决方案

Only a single doctype declaration is permitted. This follows rather directly from the HTML specifications as well the HTML5 drafts, and it can also be checked using a validator.

Thus, there is no specification of what should happen. The natural expectation is that since browsers process the doctype declaration only in "doctype sniffing" when deciding on the browser mode (Quirks Mode vs. Standards Mode), only the first doctype declaration takes effect and the other is ignored.

This can be tested e.g. as follows (using an HTML 3.2 doctype, which triggers Quirks Mode on all doctype-sniffer browsers):

<!DOCTYPE HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<title>Testing duplicate doctype effect</title>
<script>
document.write(document.compatMode);
</script>
</html>

This displays "CSS1Compat" (= Standards Mode), whereas swapping the doctype declarations causes "BackCompat" (= Quirks Mode).

这篇关于在一个HTML文档的多个文档类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:14