一个div的高度我在CSS里面没有写死,让他自动,那如果我要用js去获取他的高度,怎么写啊?<div><p>1231231231231</p></div>

解决方案 »

  1.   

    你想获取的是它自己的高度还是相对页面的高度啊
    它的高度这样就行:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script>        $(function () {
                alert($('div').height());
            })
        </script>
    </head>
    <body>
    <div>
    <p>1231231231231sdfdsdfsdfsdfasdfa</p></div>
    </body>
    </html>
      

  2.   

    <div id="contentarea">
     ......
    </div>document.getElementById("contentarea").offsetHeight
      

  3.   

    JavaScript版本:document.getElementsByTagName("div")[0].offsetHeight;Jquery版本:$("div").first().height();