触发标准模式
1、加doctype声明,比如:
< !doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd"> < !doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> < !doctype html>
2、设置x-ua-compatible触发。
触发怪异模式
1、无doctype声明、定义旧的html版本(html4以下,例如3.2)
2、加xml声明,可在ie6下触发
<?xml version="1.0" encoding="utf-8"?>
<!doctype ...>
3、在xml声明和xhtml的doctype之间加入html注释,可在ie7下触发 <?xml version="1.0" encoding="utf-8"?>
< !-- keep ie7 in quirks mode -->
< !doctype ...>
5、<!--->放在<!doctype前面
ie8有4种模式:ie5.5怪异模式、ie7标准模式、ie8几乎标准模式、ie8标准模式
x-ua-compatible设置对ie8模式的影响:
1、设置x-ua-compatible meta
ie=5、ie=6(介于5、6之间的任意数字,比如5.987654321):触发ie5怪异模式(无论页面是否有doctype)
ie=7(7<= 值 <8):强制ie7标准(无论页面是否有doctype)
ie=emulateie7:遵循doctype(有doctype-ie7标准;无doctype-ie5怪异模式)
ie=emulateie8:遵循doctype(doctype-ie8几乎标准模式(或ie8标准模式);无doctype-ie5怪异模式)
ie=8、ie=edge、ie=99、ie=9.9(大于等于8的):有doctype-使用几乎标准模式,或ie8标准;无doctype-ie8标准。
无x-ua-compatible、ie=ie8、ie=ie7、ie=a、把x-ua-compatible写在<link>或<script>标签后:判断"x-ua-compatible http header"。
注1:把x-ua-compatible写在<link>或<script>标签下面,x-ua-compatible的设置无效。
注2:页面、服务器http header都设置了x-ua-compatible的情况,使用页面的x-ua-compatible设置。页面无x-ua-compatible,才使用http header设置的值。
注3:几乎标准模式的意思和触发,下面的"almost standards mode"有说明。
注4:ie=xx的值,ie会尝试xx转换为最接近的值。比如:ie=7.789 -> ie=7;介于5、6之间的->ie=5;大于等于8的->ie=8。
注5:ie=4、ie=3、ie=0.1、ie=-7 这些小于5的,包括类似ie=ie8、ie=ie7、ie=ie6、ie=a、ie=b、ie=bcd,和无x-ua-compatible一样一样滴。可以理解为x-ua-compatible设置了无效的值,所以跳过这里了。
2、设置x-ua-compatible http header
ie=5、ie=6: 触发ie5怪异模式(无论页面是否有doctype)
ie=7(7<= 值 <8): 强制ie7标准(无论页面是否有doctype)
ie=emulateie7: 遵循doctype(有doctype-ie7标准;无doctype-ie5怪异模式)
ie=emulateie8:遵循doctype(doctype-ie8几乎标准,或ie8标准;无doctype-ie5怪异模式)
ie=8、ie=edge、ie=99、ie=9.9(大于等于8的):有doctype-使用几乎标准模式(或ie8标准);无doctype-ie8标准。
注:设置了x-ua-compatible(meta或http header)后,会覆盖客户端的兼容性视图设置。会强迫(优先)使用x-ua-compatible设置的模式
无x-ua-compatible、ie=ie8、ie=ie7、ie=a、还有上面注5里面提到的:首先判断"兼容性视图",有"兼容性视图"的设置(doctype-ie7标准,无doctype-ie5怪异模式);未设置"兼容性视图",有doctype-遵循doctype,无doctype-ie5怪异模式。
ie9有7种模式: ie5.5怪异模式、ie7标准模式、ie8几乎标准模式、ie8标准模式、ie9几乎标准模式、ie9标准模式、xml模式
ie9和ie8大体上差不多:
x-ua-compatible
ie=(0<= 值 <7) - 触发怪异模式(无论页面是否有doctype),注:这里的怪异模式和ie8下的有点不同,测试发现"-"、"_"这两个css hack符号,ie8怪异下是都识别的,ie9怪异下不识别"-"。
ie=7(7<= 值 <8) - 强制ie7标准(无论页面是否有doctype)
ie=8(8<= 值 <9) - 强制ie8标准,有doctype-使用ie8几乎标准模式(或ie8标准),无doctype-ie8标准
ie=emulateie7、emulateie8 和上面ie8的情况一样
ie=9、ie=edge(值 >=9 ) - 有doctype-使用几乎标准模式(或ie9标准)。和ie8一样,靠doctype来选择ie9几乎标准,或ie9标准模式;无doctype-ie9标准。
ie=(值 <0)、ie=ie8、ie=ie7、ie=ie6、ie=a、ie=b、ie=bcd、ie=xxx类似这样不靠谱的、或把x-ua-compatible meta写在<link>或<script>标签后的,这些情况和无x-ua-compatible是一样的:首先判断"兼容性视图",有"兼容性视图"的设置(doctype-ie7标准,无doctype-怪异模式);未设置"兼容性视图",有doctype-遵循doctype,无doctype-怪异模式。
x-ua-compatible的特殊写法
msdn上面提到了x-ua-compatible值设置成"ie=9; ie=8; ie=5"这样的,意思就是优先最前面的ie9,没ie9就用ie8,没ie8就ie5,并且并不推荐在生产环境下使用。
触发google chrome frame:<meta http-equiv="x-ua-compatible" content="chrome=1">
可以和ie的x-ua-compatible混搭:比如:<meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1">。这样写的好处:可以让ie在最好的渲染方式下渲染页面。
"ie=edge,chrome=1",可以写成"chrome=1,ie=edge"、"chrome=1; ie=edge"。
几乎标准模式(almost standards mode)
firefox 1+、safari、chrome、opera(从7.5开始)和ie8/ie9增加了一个"几乎标准模式",它实现传统的表格单元格的垂直尺寸(没有严格的遵照css2规范)。
意思就是,比如下面的代码:
<table style="border:1px solid blue;" cellspacing="0"> < tr><td><img style="border:1px solid red" width="364" height="126" src="http://www.google.com/images/logos/ps_logo2.png"/></td></tr> < /table>
比如在ie7标准模式下,图片底部和table是没空白的;"几乎标准模式"下,图片底部和table也是没空白的;而较新的浏览器在标准模式下图片底部和table会有个空白。
@see:https://developer.mozilla.org/en/images,_tables,_and_mysterious_gaps
下面的doctype都可触发ie8标准模式:
<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> < !doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd"> < !doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> < !doctype html>
触发ie8几乎标准模式:
c22098bfd3f62d7436149aaab013de1a
3c6ee2e4e556c3badc505155aae90f4f
mac ie5、ie6/7、opera(8f6e2fe385d7f17760ce5575dcfc3807)。
任何一个xhtml文档的解析错误会导致停止解析,ff/sf/ch/op会直接报xml解析错误,ie9可以在开发人员工具的console里面看到报错。
macie5、ie6/7/8不支持application/xhtml+xml。
发现ie9下使用此模式,doctype是无所谓的,1d33348365c3ec55b420fe6254061305 这句命名空间必须的,没设置命名空间css会以文本解析而失效。
同时xml模式下,x-ua-compatible的设置将会无效。
总结:
x-ua-compatible只有ie>=8才识别,所以他们可以用doctype声明、x-ua-compatible、兼容性视图设置来改变模式。
ie6/7却依靠doctype来改变标准或怪异模式,类似这些低版本的浏览器下的标准模式,接近与“先进”浏览器下的几乎标准模式,因为它们都没有严格遵循css2规范。并且他们同样在标准模式下,各自的渲染还是是有差别的。没差别就不会有css hack的诞生。
ie6-ie9下,怪异模式都在ie5.5下。
不需要写x-ua-compatible,用css也完全可以搞定各个版本ie的解析不同。
模式、版本不同,不仅仅css解析不同,js的解析也有不同。
了解浏览器解析模式的不同,可以避免我们辛苦写出的标准代码被怪异所残害。
让开发者更注重遵循标准,无论在生产效率还是在协作、沟通上都有好处滴。
现在几乎人人都用标准的doctype来声明文档,所以纠结标准、怪异模式对工作的影响不太大。
以上就是浏览器解析模式:doctype声明问题用法详解的详细内容。
