Javascript 文本区域具有固定的字符限制和行限制
HTML代码Javascript 文本区域具有固定的字符限制和行限制,javascript,html,css,Javascript,Html,Css,HTML代码 <textarea id="t1" class="txt" rows="5" cols="50" maxlength="250"></textarea> 输出 在上面的文本区域插入文本后,我得到了这个滚动条,如红色圆圈所示 我需要的是具有以下限制的文本区域: 最大长度为250个字符或最多5行 每行的最大字符数为50个字母 复制功能将被禁用,并且maxlength定义为250 我尝试了以下代码: <!DOCTYPE HTML PUBLIC "-/
<textarea id="t1" class="txt" rows="5" cols="50" maxlength="250"></textarea>
输出
在上面的文本区域插入文本后,我得到了这个滚动条,如红色圆圈所示
我需要的是具有以下限制的文本区域:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function limitlength(obj, length) {
var maxlength = length
if (obj.value.length > maxlength)
obj.value = obj.value.substring(0, maxlength)
}
function CountChars(ID) {
}
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
<style>
#TextBox2 {
resize: none;
}
input, textarea{
font-family: Arial, sans-serif;
font-size: 100%;
width: 26em; /* fallback for the next one, for browsers not recognizing ch */
width: 40ch; /* sets the width to 40 times the width of the digit “0” */
}
</style>
</head>
<body>
<div id="sampleArea">dasdasdasdasd</div>
<textarea id="TextBox2" class="txt" rows="20" cols="5" onkeypress="return limitlength(this, 20)"></textarea>
</body>
</html>
功能限制长度(obj,长度){
var maxlength=长度
if(obj.value.length>maxlength)
obj.value=obj.value.substring(0,maxlength)
}
函数CountChars(ID){
}
#文本框2{
调整大小:无;
}
输入,文本区域{
字体系列:Arial,无衬线;
字体大小:100%;
宽度:26em;/*对于不识别ch的浏览器,下一个的回退*/
宽度:40ch;/*将宽度设置为数字“0”宽度的40倍*/
}
达斯达斯达斯德
我也试过这个
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function CountChars(ID) {
var i, limit = 35, lines = [], result = '', currentLine = '';
var textBox = document.getElementById('TextBox2');
var text = textBox.value;
var words = text.split(' ');
for (i = 0; i < words.length; i++) {
var extendedLine = currentLine + ' ' + words[i];
if (extendedLine.length > limit) {
lines.push(currentLine);
currentLine = words[i];
} else {
currentLine = extendedLine;
}
}
if (currentLine.length > 0) {
lines.push(currentLine);
}
for (i = 0; i < lines.length; i++) {
result += lines[i] + '\r\n';
}
textBox.value = result;
}
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
<style>
#TextBox2 {
resize: none;
}
</style>
</head>
<body>
<div id="sampleArea">dasdasdasdasd</div>
<textarea id="TextBox2" class="txt" rows="20" cols="35" onkeyup="CountChars(this);"></textarea>
</body>
</html>
函数CountChars(ID){
var i,limit=35,lines=[],result='',currentLine='';
var textBox=document.getElementById('TextBox2');
var text=textBox.value;
var words=text.split(“”);
对于(i=0;i限制){
线路。推送(当前线路);
currentLine=字[i];
}否则{
currentLine=延长线;
}
}
如果(currentLine.length>0){
线路。推送(当前线路);
}
对于(i=0;i
您可以尝试自动溢出或隐藏溢出,如下所示
<textarea style="overflow:auto"></textarea>
<textarea style="overflow:hidden"></textarea>
您必须使用javascript实现这一点,因此请添加适当的标记。您可以在文本区域中使用style=“overflow:hidden”删除滚动条。您可能会发现这个答案很有用。您尝试了什么?你不应该期望别人为你编写代码。此外,这项任务的定义并不明确。当用户尝试键入或粘贴超过指定限制的数据时,会发生什么情况?例如,是否需要过长的换行?@JukkaK.Korpela请再次检查问题……这不会限制OP根据“我需要的是一个有以下限制的文本区域”的语句所需要的输入!“在上面的文本区域中插入文本后,我得到了这个滚动条,如红色圆圈所示。”因此我认为他不需要它们,如果他将rows=“5”cols=“50”放入文本区域,则可以在没有滚动的情况下,将所有文本放入文本区域,并分成5行。