我有一个有高度限制的 div。现在我想知道如果没有设置高度限制,div 会有多高。我怎么能得到那个?
丹尼尔
诀窍是:
.height()height为auto
$(function(){
   var $test = $('#test');        // cache element
   var orgH = $test.height();     // get LIMITED height
   $test.css({height:"auto"});    // go to non-limited height
   var couldBeH = $test.height(); // store that one
   $test.css({height: orgH});     // reset org height
   alert("Could be "+ couldBeH);
});
使用 javascript:
var $div = $('div');
var height = $div.height();
$div.css('height','auto');
var newHeight = $div.height();
$div.css('height',height);
alert('original height:' + height) ;
alert('new height:' + newHeight) ;
如果您使用的是 Jquery,那么例如您的 div id 是test那么您可以获得 height $('#test').height();。
var mydivheight = document.getElementById('myDiv').clientHeight;
var mydivheight  = document.getElementById('myDiv').offsetHeight;
var mydivheight  = document.getElementById('myDiv').scrollHeight;
clientHeight -包括高度和垂直填充。
offsetHeight -包括高度、垂直填充和垂直边框。
scrollHeight -包括包含文档的高度(在滚动的情况下将大于高度)、垂直填充和垂直边框。