我们都知道,IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有很大差别,而在不声明Doctype的情况下, IE默认又是Quirks Mode。所以为兼容性考虑,我们可能需要获取当前的文档渲染方式。
document. compatMode正好派上用场,它有两种可能的返回值: BackCompat和 CSS1Compat,对其解释如下:
BackCompat: Standards-compliant mode is not switched on. (Quirks Mode)
CSS1Compat: Standards-compliant mode is switched on. (Standards Mode)
在实际的项目中,我们还需要在获取浏览是否IE,这样就可以得到IE的渲染模式了。
在Ext中的代码:
var
ua
=
navigator.userAgent.toLowerCase();
var isStrict = document.compatMode == " CSS1Compat " ,
isIE = ! isOpera && ua.indexOf( " msie " ) > - 1 ,
isBorderBox = isIE &&! isStrict;
var isStrict = document.compatMode == " CSS1Compat " ,
isIE = ! isOpera && ua.indexOf( " msie " ) > - 1 ,
isBorderBox = isIE &&! isStrict;
当文档有了标准声明时, document. compatMode 的值就等于 "CSS1compat", 因此, 我们可以根据 document. compatMode 的值来判断文档是否加了标准声明
var
height
=
document.compatMode
==
"
CSS1Compat
"
?
document.documentElement.clientHeight : document.body.clientHeight;