Accessing the height of div in javascript

后端 未结 4 1049
无人及你
无人及你 2021-01-13 03:31

I have a div :

css

    div { width: 200px; height:auto }

markup

   
相关标签:
4条回答
  • 2021-01-13 04:15

    For returning the NUMERIC height value :

    document.getElementsById('myElementId').offsetHeight; // Without jQuery
    
    $('#myElementId').outerHeight(); // With jQuery 
    

    Note 1: outerHeight(true) returns the size with margin and padding inclued, more informations on http://api.jquery.com/outerHeight/

    Note 2 : innerHeight() returns the current computed height for the first element in the set of matched elements, including padding but not border.

    Note 3: $('div').height() or $('div').css("height") returns the css value only.

    0 讨论(0)
  • 2021-01-13 04:18

    Try This

    var divs = document.getElementsByTagName('div');
    if(divs.length>0)
         divs[0].offsetHeight;
    
    0 讨论(0)
  • 2021-01-13 04:23

    try using

    $('div').innerHeight()
    

    or

    $('div').outerHeight()
    
    0 讨论(0)
  • 2021-01-13 04:32

    You may want to try .innerHeight() or .outerHeight(), depending on what you want.

    0 讨论(0)
提交回复
热议问题