How to detect the height of the content of a div?

孤人 提交于 2019-12-11 10:44:37

问题


provided a DIV with

  • fixed heigh
  • disabled scrolling feature
  • whose content is editable

I need to understand if its content height as reached or even exceeded the height of the same div according to the element (text or other) inserted by the user (control triggered on event basis). Does anybody have any simple idea how to detect if the content of a DIV has reached or even exceeded the height of the same DIV?


回答1:


As stian said in their answer, you can use jQueries height() method to obtian the height of a given element.

However, you'll want to utilise this method on a second container internal the the main div. Such that:

<div id="myMainDivWithAHeight">
     <div id="myInnerDivExpandingWithTheContent"></div>
</div>

This way you'll be calculating the height of the inner div which is expanding in relation to the content, instead of the height of the fixed-height outter div.

You can then compare the values of the inner and outter div heights.




回答2:


try jQuery .height()

http://api.jquery.com/height/



来源:https://stackoverflow.com/questions/8988200/how-to-detect-the-height-of-the-content-of-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!