フォントサイズを下げようとしない場合は高さと幅が固定されているdivボックス内にテキストが収まるかどうかをチェックして、入力されたテキストを制限するスクリプトを書いています。私はこれを達成しましたが、高さを超えて制限に達することがあり、テキストを上書きしてから選択することはできません。
これが私のスクリプトです:
function onEdit(e) {
var notebox_tmp = $(".notebox_tmp");
var maxHeight = 120;
var minFontSize = 25;
notebox_tmp.css("font-size","50px");
notebox_tmp.text($(this).val());
var currentHeight = notebox_tmp.height();
var currentFontSize = parseInt(notebox_tmp.css("font-size"),10);
while (currentHeight > maxHeight && currentFontSize > minFontSize) {
notebox_tmp.css("font-size", currentFontSize);
currentFontSize -= 0.25;
currentHeight = notebox_tmp.height();
}
if (currentFontSize <= minFontSize) {
e.preventDefault();
}
};
$("textarea").keypress(onEdit);
これが私の現在のスクリプトデモです: http://jsfiddle.net/6pfCM/9/