gpt4 book ai didi

javascript - 具有有限行和字符限制的文本区域

转载 作者:搜寻专家 更新时间:2023-11-01 04:50:42 26 4
gpt4 key购买 nike

我需要有 TextArea 的功能

1) maximum total lines- 6 and 
2) in each line there must be maximum of 16 chars
3) if user enters 17th character the cursor should go to the next line
and user will type in there (the line will be counted)
4) if user reaches to the 7th line it will not allow user to write
5) if user type e.g "Hello, I Love StackOverflow and its features" (counting
from 1st Char 'H', the 16th char is 't' but it is whole word 'StackOverflow',
it shouldn't break and continue to next line e.g.
Hello, I Love St
ackOverflow
now the whole word should come to next line like:

Hello, I Love
StackOverflow
and its features

这是我到目前为止所做的链接 http://jsfiddle.net/nqjQ2/2/

有时某些功能有效,有时无效,并且面临 onKeyUp 和 onKeyDown 的浏览器问题谁能帮我解决这个问题?

最佳答案

我认为这主要是您想要的:

<textarea id="splitLines"></textarea>

JavaScript:

var textarea = document.getElementById("splitLines");
textarea.onkeyup = function() {
var lines = textarea.value.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lines[i].length <= 16) continue;
var j = 0; space = 16;
while (j++ <= 16) {
if (lines[i].charAt(j) === " ") space = j;
}
lines[i + 1] = lines[i].substring(space + 1) + (lines[i + 1] || "");
lines[i] = lines[i].substring(0, space);
}
textarea.value = lines.slice(0, 6).join("\n");
};

参见 fiddle in action .

关于javascript - 具有有限行和字符限制的文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14259580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com