用js实现在textarea中每输入15个数字自动换行,textarea页面样式已确定,如下图
2014032010000002014032010000012014032010000022014032010000032014032010000042014032010...
201403201000000
201403201000001
201403201000002
201403201000003
201403201000004
201403201000005
201403201000006
201403201000007
201403201000008
201403201000009
201403201000010
201403201000011
201403201000012
201403201000013
201403201000014
201403201000015
201403201000016
201403201000017
201403201000018
201403201000019
201403201000020
201403201000021
201403201000022
201403201000023
201403201000024
201403201000025
201403201000026
201403201000027
201403201000028
201403201000029 展开
201403201000001
201403201000002
201403201000003
201403201000004
201403201000005
201403201000006
201403201000007
201403201000008
201403201000009
201403201000010
201403201000011
201403201000012
201403201000013
201403201000014
201403201000015
201403201000016
201403201000017
201403201000018
201403201000019
201403201000020
201403201000021
201403201000022
201403201000023
201403201000024
201403201000025
201403201000026
201403201000027
201403201000028
201403201000029 展开
1个回答
展开全部
<textarea style="width: 200px;height: 200px;"></textarea>
<script>
document.getElementsByTagName('textarea')[0].onkeypress = function () {
var value = this.value,
allLine = value.split('\n'),
lastLine = allLine.pop();
if (lastLine.length >= 15) {
lastLine = lastLine.substr(0, 15) + '\n' + lastLine.substr(15);
allLine.push(lastLine)
this.value = allLine.join('\n');
}
};
</script>
更多追问追答
追问
谢谢,麻烦您帮帮忙,textarea中输入的必须是数字,并且不能有空格,可以直接粘帖进去,但是必须要保证格式
追答
var textarea = document.getElementsByTagName('textarea')[0],
timer,
fn = function () {
var v = textarea.value,
value = v.replace(/[^\d]/g, ''),
newValue = [];
while (value) {
newValue.push(value.substr(0, 15));
value = value.substr(15);
}
newValue = newValue.join('\n');
if (v != newValue) {
textarea.value = newValue;
}
};
textarea.onfocus = function () {
timer = setInterval(fn, 100);
};
textarea.onblur = function () {
clearInterval(timer);
fn();
};
textarea.onkeypress = fn;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询