这是另一种方法。虽然效率较低,但它可能比我的其他答案更准确。
看这里的例子:
基本上将宽度向下循环,直到达到所需的高度
<div id="AA">
<div id='A'>
I dont know how wide i am, I dont know how wide i am, I dont know how wide i am, I dont know how wide i am, I dont know how wide i need to be
</div>
</div>
CSS:
#AA {
height: 300px;
border: 1px solid black;
width: 100%;
}
#A {
display: inline;
}
剧本:
height = $('#AA').height();
minWidth = 300;
currentWidth = $('#A').width();
currentHeight = $('#A').height();
stop = false;
if (currentWidth >= minWidth) {
while (stop === false) {
if (currentWidth >= minWidth) {
newWidth = currentWidth - 1;
$('#AA').width(newWidth);
currentHeight = $('#A').height();
currentWidth = $('#A').width();
if (currentHeight < height) {
stop = false;
}
else {
stop = true;
}
}
else {
stop = true;
}
}
}