前台有一个<div id="content">div里面的内容不是固定的,所以高度也不是固定的,在DIV的样式里面也没有写height属性;然而在JavaScript里面怎么取它的当前高度?我试过用JQuery的$("#content").css("height")去取,但是取出来的是auto;也试过用document.getElementById("content").style.height去取,取出来是一个空字符串;我的问题就是:怎么取它当前的具体高度? 比如400px.

解决方案 »

  1.   

    $("#content").height();
    $("#content").innerHeight();//元素内部区域高度,忽略padding、border
    $("#content").outerHeight();//忽略边框
    $("#content").outerHeight(true);//包含边框高度
      

  2.   

    css()方法获取的高度值与样式的设置有关,可能会得到"auto",也可能是"10px"之类的字符串,而height()方法获取的高度则是元素在页面中的实际高度,与样式的设置无关,并且不带单位
    来自《锋利的query》90页,希望对LZ有帮助
      

  3.   

    lz 你应该先分清CSS中 设置的值和计算后的值,你想要的应该是它计算后的高度
    style可以获取你设置的值
    想要获取计算后的值,在IE中使用currentStyle,其他浏览器里用document.getComputedStyle
    自己写方法的话,是这样的:
    function getStyle(){ 
    return document.defaultView?document.defaultView.getComputedStyle(obj,null).getPropertyValue(style):obj.currentStyle[style.replace(/-[a-z]/g,function(){return arguments[0].charAt(1).toUpperCase();})]; 
    }如果是用Jquery中的方法,应该是:
    $("#content").height();
      

  4.   

    通常获得高度的时候是用offsetHeight来计算的,而不是css
      

  5.   

    试了 $("#content").height();  
    结果还是 auto
    囧.....
      

  6.   

    楼主别理这群读书读傻掉的家伙,越扯越远,javascript获得DIV高度:
    window.document.getElementById("y1yx2").clientHeight
      

  7.   

        window.onload=function(){
         kuan=window.document.getElementById("y1yx2").clientHeight
         if (kuan<296){window.document.getElementById("y1yx2").style.height=296}
        }