带溢出的div的内容高度:auto

时间:2011-08-08 19:30:19

标签: javascript css

如何通过javascript找出设置高度和溢出的div内容的高度:auto?

1 个答案:

答案 0 :(得分:1)

您可以将内容包装在另一个div中,然后您可以在此内部div上使用.offsetHeight ...

<!DOCTYPE HTML>
<html>
  <body>
    <div style="background-color:#EEEEEE; height:400px; overflow:auto; width:100%">
      <div id="container">
        <p>The quick brown fox jumps over the lazy dog</p>
        <p>The quick brown fox jumps over the lazy dog</p>
        ... snip ...
        <p>The quick brown fox jumps over the lazy dog</p>
        <p>The quick brown fox jumps over the lazy dog</p>
      </div>
    </div>
    <script>

setTimeout(function() {
    var container = document.getElementById("container");
    alert("Container height = " + container.offsetHeight);
}, 1000);

    </script>
  </body>
</html>