2017-03-28 51 views
1

我想知道div的真實高度。獲取div的真實高度(jquery)

如果我要求$var.height()他只是告訴我修復CSS高度。

我怎樣才能得到div的真正高度,而不是固定的CSS高度。

+1

你是什麼意思'真實高度?你嘗試過'$ var.outerHeight()'嗎? – Justinas

+0

我給一個div例如css高度500px。現在它獲得內容。內容可能會超過500px,所以我要求身高,但總是告訴我500 – AskLo

+0

@AskLo你說的***根本沒有意義***。如果您爲任何元素設置了固定高度,則只會將其固定爲該設置值,不能根據其內容大小縮小/擴大。如果你的意思是你想獲得一個元素的動態/靈活的大小,那麼應該使用'.outerHeight()'或'height()'(它基於'getComputedStyle')。 –

回答

0

你試過outerHeight(),可能這會幫助你,試試這個

$(document).ready(function() { 
 
    $("button").click(function() { 
 
    alert("Outer height of div: " + $("div").outerHeight()); 
 
    }); 
 
});
div { 
 
    height: 100px; 
 
    width: 300px; 
 
    padding: 10px; 
 
    margin: 3px; 
 
    border: 1px solid blue; 
 
    background-color: lightblue; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 

 
<div></div><br> 
 

 
<button>Display the outer height of div</button> 
 
<p>outerHeight() - returns the outer height of an element (includes padding and border).</p>

+0

OuterHeight allways給我固定的css高度+ margin/padding但不是真正的高度。我有一個修復高度的CSS。 – AskLo