我需要擴大溢出設置爲通過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