Get div height with plain JavaScript

前端 未结 10 933
梦毁少年i
梦毁少年i 2020-11-22 17:22

Any ideas on how to get a div\'s height without using jQuery?

I was searching Stack Overflow for this question and it seems like every answer is pointing to jQuery\'

10条回答
  •  感动是毒
    2020-11-22 17:45

    In addition to el.clientHeight and el.offsetHeight, when you need the height of the content inside the element (regardless of the height set on the element itself) you can use el.scrollHeight. more info

    This can be useful if you want to set the element height or max-height to the exact height of it's internal dynamic content. For example:

    var el = document.getElementById('myDiv')
    
    el.style.maxHeight = el.scrollHeight+'px'
    

提交回复
热议问题