我使用的是站点的绝对定位。用户有个人资料,他们写的文字数量会有所不同。
如何根据所显示的文本量动态更新div
元素的高度。更改div
高度以动态适应内容。
<div class="profileContainer">
<h3 class="profileText">
Here is the text - this could be 1000px height plus
</h3>
</div>
我知道jQuery height()
方法,但我不确定如何计算给它多少个数字-即。如何找出文本的高度。
发布于 2012-09-03 20:28:29
在页面加载时(或者在更新h3
元素之后,如果发出了某种AJAX请求),只需将#profileContainer
的高度设置为等于#profileText
的高度。
试试这个:
$("#profileContainer").height($("#profileText").height());
当然,这是假设您已经在CSS中为容器div
设置了特定的height
。如果您没有这样做,那么h3
的内容将自动导致容器按需增长。
发布于 2012-09-03 20:31:34
如果您的目的是显示所有内容,那么下面的css就可以完成此任务。
div.profileContainer {height:auto; width:300px;}
这样,容器的大小将自动增长以适应内容,宽度将被限制为300px
发布于 2012-09-03 20:32:40
你可以用css
做到这一点...
<div class="profileContainer" style="height:auto;">
<h3 class="profileText">
Here is the text - this could be 1000px height plus
</h3>
</div>
https://stackoverflow.com/questions/12247838
复制相似问题