做这个...
html:
<input type="text" id="toprightlefthighbox" class="idea" value="" />
Javascript:
var MAX_FONT = 20;
var MIN_FONT = 10;
var YOUR_STRING = "Here is my long long long long string";
var textbox = document.getElementById("toprightlefthighbox");
var fontSize = MAX_FONT + 1;
var element = document.createElement("div");
element.style.visibility = "hidden";
element.style.display = "inline-block";
element.style.padding = "0px";
element.style.fontFamily = "Arial";
element.innerHTML = YOUR_STRING;
document.body.appendChild(element);
do
{
fontSize -= 1; //maybe -= 0.5 in some browsers? Just change the +1 above too
element.style.fontSize = fontSize + "px";
} while (fontSize > MIN_FONT && element.clientWidth > textbox.clientWidth);
document.body.removeChild(element);
element = null;
textbox.value = YOUR_STRING;
textbox.style.fontSize = fontSize + "px";
使用 CSS:
#toprightlefthighbox { width: 205px; height: 25px; font-family: arial; }
JSF中:
http://jsfiddle.net/hkN35/4/
请注意,当您将文本框的宽度减小到 200 像素时,它会降低字体大小。