【发布时间】:2010-11-10 10:09:46
【问题描述】:
我在这里问了一个类似的问题: How to auto resize text in fixed DIV according to the text's length?
及其工作。 但它在单行文本上工作。 假设我希望我的文本换行到 2 行,如果它更多(溢出)然后去调整大小......
有没有办法做到这一点?
谢谢!
【问题讨论】:
标签: javascript html css
我在这里问了一个类似的问题: How to auto resize text in fixed DIV according to the text's length?
及其工作。 但它在单行文本上工作。 假设我希望我的文本换行到 2 行,如果它更多(溢出)然后去调整大小......
有没有办法做到这一点?
谢谢!
【问题讨论】:
标签: javascript html css
如果你知道字体大小、字体样式和行高,我想你知道它是否是你的网站:)。您可以编写 javascript 函数,将字符添加到 div 直到它的高度小于或等于两行高度。
这是适用于所有浏览器的单行检查示例。 说实话,这不是我的解决方案,但我的同事允许发布它:)。
function fitInWidth(name) {
this.empty().append(name);
while (this.height() > 19) {
name = name.substring(0, name.length - 3);
while (name.charAt(name.length - 1) == ' ') name = name.substring(0, name.length - 2);
this.empty().append(name = name + '…');
}
return name;
}
【讨论】:
可以创建 div inline 然后找到高度,然后创建 block 但使用新高度。
$("#element").css("display","inline").css({height: $("#element").height(), display: "block"});
【讨论】: