2010-07-19 75 views
1

在我使用document.body.clientHeight得到身體的高度的JavaScript函數返回的值不同。現在取決於IE8的模式(即怪癖或標準),這個值是不同的。clientHeight取決於模式IE8是

例 在怪癖,document.body.clientHeight = 800像素 在標準的,document.body.clientHeight = 650像素

希望我已經是有意義的。

請幫忙。

回答

4

怪癖模式和標準模式返回值是不一致的。既然你標記它作爲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"); 
    }; 

}); 
+1

當使用jQuery功能我STILE得到不同的值。任何其他想法? 在怪異 $( '主體')。寬度= 1239 $( '主體')。高度= 184 document.body.clientWidth = 1231 document.body.clientHeight = 176 在標準 $(」 body')。width = 1260 $('body')。height = 182 document.body.clientWidth = 1254 document.body.clientHeight = 176 – 2010-07-20 08:21:23

+0

爲什麼不行格式爲:( – 2010-07-20 08:22:14

4

IE實際上返回正確的clientHeightclientWidth在兩種模式下的值 - 但它以不同方式呈現盒子的高度/寬度。請參閱Quirks Mode瞭解相關演示。

在怪癖模式下,IE渲染寬度/高度的填充和邊界在框的寬度/高度內 - 在標準模式下,IE正確渲染填充和邊框以及框的聲明寬度。

,以確保一致性的最好方法是強制IE爲標準模式,通過包括在你的文件的開頭這樣定義:

<!doctype html>