2012-07-16 33 views
5

我有一個div:訪問div的高度在JavaScript

CSS

div { width: 200px; height:auto } 

標記

<div contenteditable="true"> Text is editable </div> 

現在我應該怎麼做才能訪問的的height (numeric value)以上div在JavaScript?我試過

$('div').height() & $('div').css("height");兩者都返回auto

+3

兩者都很好http://jsfiddle.net/sySFk/ – Musa 2012-07-16 08:06:10

回答

1

嘗試使用

$('div').innerHeight() 

$('div').outerHeight() 
1

嘗試這個

var divs = document.getElementsByTagName('div'); 
if(divs.length>0) 
    divs[0].offsetHeight; 
0

對於返回NUMERIC高度值

document.getElementsById('myElementId').offsetHeight; // Without jQuery 

$('#myElementId').outerHeight(); // With jQuery 

注1:outerHeight(真)返回上http://api.jquery.com/outerHeight/

注2 margin和padding inclued大小,更詳細的信息:innerHeight()返回用於第一元件的電流計算出的高度中的一組匹配元素,包括填充但不包含邊框。注意3:$('div')。height()或$('div').css(「height」)僅返回css值。