代码之家  ›  专栏  ›  技术社区  ›  Ash Burlaczenko

根据IE8所处的模式,clientheight返回不同的值

  •  1
  • Ash Burlaczenko  · 技术社区  · 16 年前

    在一个javascript函数中,我使用document.body.clientheight来获取主体的高度。现在,根据IE8所处的模式(即要求或标准),该值不同。

    例子 在quirks中,document.body.clientheight=800px 在标准中,document.body.clientheight=650px

    希望我说得通。

    请帮忙。

    2 回复  |  直到 13 年前
        1
  •  4
  •   meder omuraliev    16 年前

    quirks模式和标准模式返回值不一致。既然您将其标记为jquery,那么只需使用

    $('body').height() $(window).height() 取决于你需要什么…它在内部对其进行了修复,因此您不必键入以下内容:

    jQuery.each([ "Height", "Width" ], function( i, name ) {
    
        var type = name.toLowerCase();
    
        // innerHeight and innerWidth
        jQuery.fn["inner" + name] = function() {
            return this[0] ?
                jQuery.css( this[0], type, false, "padding" ) :
                null;
        };
    
        // outerHeight and outerWidth
        jQuery.fn["outer" + name] = function( margin ) {
            return this[0] ?
                jQuery.css( this[0], type, false, margin ? "margin" : "border" ) :
                null;
        };
    
        jQuery.fn[ type ] = function( size ) {
            // Get window width or height
            var elem = this[0];
            if ( !elem ) {
                return size == null ? null : this;
            }
    
            if ( jQuery.isFunction( size ) ) {
                return this.each(function( i ) {
                    var self = jQuery( this );
                    self[ type ]( size.call( this, i, self[ type ]() ) );
                });
            }
    
            return ("scrollTo" in elem && elem.document) ? // does it walk and quack like a window?
                // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
                elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
                elem.document.body[ "client" + name ] :
    
                // Get document width or height
                (elem.nodeType === 9) ? // is it a document
                    // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
                    Math.max(
                        elem.documentElement["client" + name],
                        elem.body["scroll" + name], elem.documentElement["scroll" + name],
                        elem.body["offset" + name], elem.documentElement["offset" + name]
                    ) :
    
                    // Get or set width or height on the element
                    size === undefined ?
                        // Get width or height on the element
                        jQuery.css( elem, type ) :
    
                        // Set the width or height on the element (default to pixels if value is unitless)
                        this.css( type, typeof size === "string" ? size : size + "px" );
        };
    
    });
    
        2
  •  4
  •   Dexter    14 年前

    ie实际返回的值正确 clientHeight clientWidth 在这两种模式下-但是它呈现的盒子的高度/宽度不同。见 Quirks Mode 为了演示这个。

    在Quirks模式下,即在框的宽度/高度范围内渲染填充和边框的宽度/高度-在标准模式下,即除了声明的框的宽度外,还正确渲染填充和边框。

    确保一致性的最佳方法是强制IE进入标准模式,方法是在文件开头包含此定义:

    <!doctype html>
    
    推荐文章