0
这里是我的代码:如何设置元素的高度与其内容相同?
$("#topbar").click(function() {
$(this).animate({
height: ($(this).height() == 45) ? 20 : 45
}, 200);
});
#topbar {
background: orange;
color: white;
display:block;
width:100%;
text-align:center;
height:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='topbar'>
toggle me
<br>
some explanations
<br>
some more explanation
</div>
正如你看到的,当你点击橙色框,其高度将增加(反之亦然)。但是45
对于当前内容来说还不够高。其实这个盒子的内容是动态的。所以我想要设置一个与其内容高度相同的高度。
我该怎么做?
'高度:($(本).height()== 20)? this.scrollHeight:20' – Jai