将高度添加到动态创建的div

时间:2011-06-17 17:15:57

标签: jquery css height jquery-animate

我没有运气试图为动态创建的div添加一些高度。

此代码获取.healcode div的高度,该div是动态生成并嵌套在#scheduleArea中,然后将#scheduleArea设置为该高度的动画。我希望能够为.healcode生成的任何高度添加额外的40px。

这是没有额外高度的代码:

function () { 
    $j("#scheduleArea").animate({height: $j('.healcode').css('height')}, {queue:false, duration: 1100, easing: 'easeOutBounce'}) 
        }, 

.css方法的文档指出“+ = 40”应该给我我正在寻找的东西,但它却将高度归零。

想法?

3 个答案:

答案 0 :(得分:3)

我想我理解你的问题。你正试图这样做?

function () { 
    $j("#scheduleArea").animate({height: $j('.healcode').css('height','+=40')}, {queue:false, duration: 1100, easing: 'easeOutBounce'}) 
}, 

如果是这样,那么问题是$j('.healcode').css('height','+=40')会将.healcode的高度增加40,但是它会返回jquery对象,而不是新的高度。因此,您尝试将#scheduleArea的高度设置为[object],将其变为0.请尝试使用此选项:

function () { 
    $j("#scheduleArea").animate({height: $j('.healcode').css('height','+=40').height()}, {queue:false, duration: 1100, easing: 'easeOutBounce'}) 
}, 

请注意在向高度添加40之后对.height的额外调用。

http://jsfiddle.net/CFCnZ/

注意:与其他答案不同,我将问题解释为您希望实际将高度添加到.healcode元素,而不仅仅是容器。不确定哪个是正确的。

答案 1 :(得分:0)

function () { 
    $j("#scheduleArea").animate({height: $j('.healcode').css('height') + 40}, {queue:false, duration: 1100, easing: 'easeOutBounce'}) 
}

答案 2 :(得分:0)

height: ( $j('.healcode').height()) + 40 ) + 'px' ;

parseInt()是必需的,因此您可以对该值执行数学运算。