我有一个可编辑的文本区域。当我们在textArea
中输入文字时,文字的字体大小会随着文字长度的变化而变化。文本长度越大,font-size
越小。这会导致对齐出现问题,有什么方法可以让文本区域内的文本对齐。
这是 JavaScript 代码:
var textie = document.getElementById('new1');
var spanny = document.getElementById('length');
var textLength;
textie.addEventListener('keypress',function(e){
var size;
textLength = textie.value.length;
size = Change(textLength);
this.style.fontSize = size;
},false);
function Change(fontLength)
{
var fontSize;
if(fontLength>=1 && fontLength<=4)
{
fontSize = 35+"px";
}
else if(fontLength>=5 && fontLength<=12)
{
fontSize = 30+"px";
}
else if(fontLength>=13 && fontLength<=35)
{
fontSize = 20+"px";
}
else if(fontLength>=36 && fontLength<=50)
{
fontSize = 18+"px";
}
else if(fontLength>=51 && fontLength<=150)
{
fontSize = 16+"px";
}
else if(fontLength>150)
{
fontSize = 15+"px";
}
return fontSize;
}
这是 HTML
<textArea class = "new" id = "new1"></textArea>
<span id = "length"></span>
I am attaching a jsfiddle, try adding (lots of) random text in it and you will understand
谢谢!
最佳答案
您的脚本没有任何问题。实际上,主流浏览器不支持 TextArea 文本对齐。
关于javascript - 具有可变字体大小的 textArea 中的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20285866/