我需要扩大溢出设置为通过CSS隐藏的股利。当点击«展开»时,div的高度被扩展到显示整个文本所需的高度。要做到这一点,我用这个代码:扩大div滚动高度
$(".expand").click(function() {
$('#mainDiv').animate({
height: $('#mainDiv').get(0).scrollHeight
}, 500, function(){
$(this).height('auto');
});
});
这工作与这样的硬编码款预期:
<p class="expand">Expand …</p>
现在我想插入这一段动态取决于是否文本在div比合适div更长。如果它小于divs设定的高度,则不需要这一段。所以我试图通过添加一个函数来实现这一点,因为代码现在看起来像这样:
$(document).ready(function() {
var height = $('#mainDiv').prop('scrollHeight');
if(height>420){
$("<p class='expand'>Expand …</p>").insertAfter('#mainDiv');
}
});
$(".aufklappen").click(function() {
$('#hauptTextInhaltReduziert').animate({
height: $('#hauptTextInhaltReduziert').get(0).scrollHeight
}, 500, function(){
$(this).height('auto');
});
});
插入段落好吧。但是:它不起作用,点击段落时没有任何反应。
帮助非常感谢。
你可以创建一个jsfiddle。 – frnt