如果只是单纯地想限制 textarea 中的字数,不想写太多的话,可用:
<textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea>
或
<textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea>
在 textarea 的 onkeyup 中检查字数之方法:
复制代码 代码如下:
<script language="javascript" type="text/javascript">
function DjCheckMaxlength(oInObj)
{
var iMaxLen = parseInt(oInObj.getAttribute('maxlength'));
var iCurLen = oInObj.value.length;
if ( oInObj.getAttribute && iCurLen > iMaxLen )
{
oInObj.value = oInObj.value.substring(0, iMaxLen);
}
} //@ END OF DjCheckMaxlength()
</script>
<textarea maxlength="80" onkeyup="return DjCheckMaxlength(this);"></textarea>
o 设定好自动去检查所有的 textarea 之方法:
复制代码 代码如下:
<head>
<script language="javascript" type="text/javascript">
function DjTextArea_AddMaxlengthAttr()
{
var aX = document.getElementsByTagName('textarea');
for ( var iI = 0; aX.length > iI; iI++ )
{
if ( aX[iI].getAttribute('maxlength') )
{
aX[iI].onkeyup = aX[iI].onchange = DjTextArea_CheckMaxlength;
}
}
} //@ END OF DjTextArea_AddMaxlengthAttr()
function DjTextArea_CheckMaxlength()
{
var iMaxLen = parseInt(this.getAttribute('maxlength'));
var iCurLen = this.value.length;
if ( this.getAttribute && iCurLen > iMaxLen )
{
this.value = this.value.substring(0, iMaxLen);
}
} //@ END OF DjTextArea_CheckMaxlength()
</script>
</head>
<body onload="DjTextArea_AddMaxlengthAttr();">
复制代码 代码如下:
var Inputs = {
OnIntegerOnly: function() {
var k = System.isIE ? event.keyCode : event.which;
if(event.ctrlKey || event.ctrlAlt || event.shiftKey) {
Inputs.DisableInput(event);
return;
}
if ( ( k < 48 || k > 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& ( k < 96 || k > 105 )
) {
Inputs.DisableInput(event);
}
return k;
},
OnDecimalOnly: function() {
var k = System.isIE ? event.keyCode : event.which;
if(event.ctrlKey || event.ctrlAlt || event.shiftKey) {
Inputs.DisableInput(event);
return;
}
if( ( k < 48 || k > 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 190 != k
&& 110 != k
&& ( k < 96 || k > 105 )
) {
Inputs.DisableInput(event);
}
return k;
},
OnAsciiOnly: function() {
var k = System.isIE ? event.keyCode : event.which;
if ( ( k < 97 || k > 122 )
&& ( k < 65 || k > 90 )
&& ( k < 48 || k > 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
Inputs.DisableInput(event);
}
return k;
},
DisableInput: function(event) {
if(System.isIE)
event.returnValue = false;
else
event.preventDefault();
},
AddOnPos: function(obj, charvalue) {
//obj代表要插入字符的输入框
//value代表要插入的字符
obj.focus();
var r = document.selection.createRange();
var ctr = obj.createTextRange();
var i;
var s = obj.value;
//注释掉的这种方法只能用在单行的输入框input内
//对多行输入框textarea无效
//r.setEndPoint("StartToStart", ctr);
//i = r.text.length;
//取到光标位置----Start----
var ivalue = "&^asdjfls2FFFF325%$^&";
r.text = ivalue;
i = obj.value.indexOf(ivalue);
r.moveStart("character", -ivalue.length);
r.text = "";
//取到光标位置----End----
//插入字符
obj.value = s.substr(0,i) + charvalue + s.substr(i,s.length);
ctr.collapse(true);
ctr.moveStart("character", i + charvalue.length);
ctr.select();
},
MaxLength: function() {
var _this = event.srcElement;
var iMaxLen = parseInt(_this.getAttribute('maxlength'));
switch(event.type) {
case 'beforepaste': {
event.returnValue = false;
} break;
case 'paste': {
var iTxtLen = _this.value.lenUTF8();
var txtCbd = window.clipboardData.getData("Text");
var iCbdLen = txtCbd.lenUTF8();
if((iMaxLen - iTxtLen) <= iCbdLen) {
var iSurplusLen = iMaxLen - iTxtLen;
var subStr = txtCbd.substr(0, iSurplusLen);
var insertStr = '';
for(var i = 0; i < subStr.length; i++) {
var chr = subStr.substr(i, 1);
if(iSurplusLen >= chr.lenUTF8()) {
insertStr += chr;
iSurplusLen -= chr.lenUTF8();
}
}
Inputs.AddOnPos(_this, insertStr);
//_this.style.backgroundColor = 'red';
//System.setTimeout(Inputs.ColorTips, 500, _this);
event.returnValue = false;
event.srcElement.style.imeMode = 'disabled';
} else {
event.returnValue = true;
event.srcElement.style.imeMode = 'auto';
}
} break;
case 'keypress': {
var iTxtLen = _this.value.lenUTF8();
if(iMaxLen <= iTxtLen) {
while(_this.value.lenUTF8() > iMaxLen) {
_this.value = _this.value.substr(0, _this.value.length - 1);
}
Inputs.DisableInput(event);
if('disabled' != event.srcElement.style.imeMode) {
event.srcElement.style.imeMode = 'disabled';
}
}
else {
if('auto' != event.srcElement.style.imeMode) {
event.srcElement.style.imeMode = 'auto';
}
}
} break;
case 'keyup': {
var k = System.isIE ? event.keyCode : event.which;
if (8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
if(_this.getAttribute) {
_this.fireEvent('onkeypress');
}
}
} break;
case 'keydown': {
var k = System.isIE ? event.keyCode : event.which;
if (8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
var iTxtLen = _this.value.lenUTF8();
if(iMaxLen <= iTxtLen) {
Inputs.DisableInput(event);
}
}
} break;
}
},
TextAreaMaxLength: function() {
var aX = document.getElementsByTagName('textarea');
for(var i = 0; i < aX.length; i++) {
if(aX[i].getAttribute('maxlength')) {
aX[i].detachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].detachEvent('onpaste', Inputs.MaxLength);
aX[i].detachEvent('onkeypress', Inputs.MaxLength);
aX[i].detachEvent('onkeyup', Inputs.MaxLength);
aX[i].detachEvent('onkeydown', Inputs.MaxLength);
aX[i].attachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].attachEvent('onpaste', Inputs.MaxLength);
aX[i].attachEvent('onkeypress', Inputs.MaxLength);
aX[i].attachEvent('onkeyup', Inputs.MaxLength);
aX[i].attachEvent('onkeydown', Inputs.MaxLength);
/*
var res = 'zh-cn' == window.navigator.systemLanguage
? '最大长度:'
: 'Maximum length for: ';
if('' == aX[i].title) aX[i].title = res + aX[i].getAttribute('maxlength');
*/
}
}
},
TextBoxMaxLength: function() {
var aX = document.getElementsByTagName('input');
for(var i = 0; i < aX.length; i++) {
var maxLen = aX[i].getAttribute('maxlength');
if('text' == aX[i].type
&& null != maxLen
&& '' != maxLen
&& undefined != maxLen
&& 2147483647 > parseInt(maxLen)
&& '' == aX[i].title) {
aX[i].detachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].detachEvent('onpaste', Inputs.MaxLength);
aX[i].detachEvent('onkeypress', Inputs.MaxLength);
aX[i].detachEvent('onkeyup', Inputs.MaxLength);
aX[i].attachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].attachEvent('onpaste', Inputs.MaxLength);
aX[i].attachEvent('onkeypress', Inputs.MaxLength);
aX[i].attachEvent('onkeyup', Inputs.MaxLength);
/*
var res = 'zh-cn' == window.navigator.systemLanguage
? '最大长度:'
: 'Maximum length for: ';
aX[i].title = res + aX[i].getAttribute('maxlength');
*/
}
}
}
}
Tools.Inputs = Inputs;
window.attachEvent('onload', Tools.Inputs.TextAreaMaxLength);
window.attachEvent('onload', Tools.Inputs.TextBoxMaxLength);
<textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea>
或
<textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea>
在 textarea 的 onkeyup 中检查字数之方法:
复制代码 代码如下:
<script language="javascript" type="text/javascript">
function DjCheckMaxlength(oInObj)
{
var iMaxLen = parseInt(oInObj.getAttribute('maxlength'));
var iCurLen = oInObj.value.length;
if ( oInObj.getAttribute && iCurLen > iMaxLen )
{
oInObj.value = oInObj.value.substring(0, iMaxLen);
}
} //@ END OF DjCheckMaxlength()
</script>
<textarea maxlength="80" onkeyup="return DjCheckMaxlength(this);"></textarea>
o 设定好自动去检查所有的 textarea 之方法:
复制代码 代码如下:
<head>
<script language="javascript" type="text/javascript">
function DjTextArea_AddMaxlengthAttr()
{
var aX = document.getElementsByTagName('textarea');
for ( var iI = 0; aX.length > iI; iI++ )
{
if ( aX[iI].getAttribute('maxlength') )
{
aX[iI].onkeyup = aX[iI].onchange = DjTextArea_CheckMaxlength;
}
}
} //@ END OF DjTextArea_AddMaxlengthAttr()
function DjTextArea_CheckMaxlength()
{
var iMaxLen = parseInt(this.getAttribute('maxlength'));
var iCurLen = this.value.length;
if ( this.getAttribute && iCurLen > iMaxLen )
{
this.value = this.value.substring(0, iMaxLen);
}
} //@ END OF DjTextArea_CheckMaxlength()
</script>
</head>
<body onload="DjTextArea_AddMaxlengthAttr();">
复制代码 代码如下:
var Inputs = {
OnIntegerOnly: function() {
var k = System.isIE ? event.keyCode : event.which;
if(event.ctrlKey || event.ctrlAlt || event.shiftKey) {
Inputs.DisableInput(event);
return;
}
if ( ( k < 48 || k > 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& ( k < 96 || k > 105 )
) {
Inputs.DisableInput(event);
}
return k;
},
OnDecimalOnly: function() {
var k = System.isIE ? event.keyCode : event.which;
if(event.ctrlKey || event.ctrlAlt || event.shiftKey) {
Inputs.DisableInput(event);
return;
}
if( ( k < 48 || k > 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 190 != k
&& 110 != k
&& ( k < 96 || k > 105 )
) {
Inputs.DisableInput(event);
}
return k;
},
OnAsciiOnly: function() {
var k = System.isIE ? event.keyCode : event.which;
if ( ( k < 97 || k > 122 )
&& ( k < 65 || k > 90 )
&& ( k < 48 || k > 57 )
&& 8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
Inputs.DisableInput(event);
}
return k;
},
DisableInput: function(event) {
if(System.isIE)
event.returnValue = false;
else
event.preventDefault();
},
AddOnPos: function(obj, charvalue) {
//obj代表要插入字符的输入框
//value代表要插入的字符
obj.focus();
var r = document.selection.createRange();
var ctr = obj.createTextRange();
var i;
var s = obj.value;
//注释掉的这种方法只能用在单行的输入框input内
//对多行输入框textarea无效
//r.setEndPoint("StartToStart", ctr);
//i = r.text.length;
//取到光标位置----Start----
var ivalue = "&^asdjfls2FFFF325%$^&";
r.text = ivalue;
i = obj.value.indexOf(ivalue);
r.moveStart("character", -ivalue.length);
r.text = "";
//取到光标位置----End----
//插入字符
obj.value = s.substr(0,i) + charvalue + s.substr(i,s.length);
ctr.collapse(true);
ctr.moveStart("character", i + charvalue.length);
ctr.select();
},
MaxLength: function() {
var _this = event.srcElement;
var iMaxLen = parseInt(_this.getAttribute('maxlength'));
switch(event.type) {
case 'beforepaste': {
event.returnValue = false;
} break;
case 'paste': {
var iTxtLen = _this.value.lenUTF8();
var txtCbd = window.clipboardData.getData("Text");
var iCbdLen = txtCbd.lenUTF8();
if((iMaxLen - iTxtLen) <= iCbdLen) {
var iSurplusLen = iMaxLen - iTxtLen;
var subStr = txtCbd.substr(0, iSurplusLen);
var insertStr = '';
for(var i = 0; i < subStr.length; i++) {
var chr = subStr.substr(i, 1);
if(iSurplusLen >= chr.lenUTF8()) {
insertStr += chr;
iSurplusLen -= chr.lenUTF8();
}
}
Inputs.AddOnPos(_this, insertStr);
//_this.style.backgroundColor = 'red';
//System.setTimeout(Inputs.ColorTips, 500, _this);
event.returnValue = false;
event.srcElement.style.imeMode = 'disabled';
} else {
event.returnValue = true;
event.srcElement.style.imeMode = 'auto';
}
} break;
case 'keypress': {
var iTxtLen = _this.value.lenUTF8();
if(iMaxLen <= iTxtLen) {
while(_this.value.lenUTF8() > iMaxLen) {
_this.value = _this.value.substr(0, _this.value.length - 1);
}
Inputs.DisableInput(event);
if('disabled' != event.srcElement.style.imeMode) {
event.srcElement.style.imeMode = 'disabled';
}
}
else {
if('auto' != event.srcElement.style.imeMode) {
event.srcElement.style.imeMode = 'auto';
}
}
} break;
case 'keyup': {
var k = System.isIE ? event.keyCode : event.which;
if (8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
if(_this.getAttribute) {
_this.fireEvent('onkeypress');
}
}
} break;
case 'keydown': {
var k = System.isIE ? event.keyCode : event.which;
if (8 != k
&& 9 != k
&& 46 != k
&& 37 != k
&& 39 != k
&& 16 != k
&& 36 != k
&& 35 != k
&& 17 != k
&& 13 != k
&& 189 != k
&& 187 != k
) {
var iTxtLen = _this.value.lenUTF8();
if(iMaxLen <= iTxtLen) {
Inputs.DisableInput(event);
}
}
} break;
}
},
TextAreaMaxLength: function() {
var aX = document.getElementsByTagName('textarea');
for(var i = 0; i < aX.length; i++) {
if(aX[i].getAttribute('maxlength')) {
aX[i].detachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].detachEvent('onpaste', Inputs.MaxLength);
aX[i].detachEvent('onkeypress', Inputs.MaxLength);
aX[i].detachEvent('onkeyup', Inputs.MaxLength);
aX[i].detachEvent('onkeydown', Inputs.MaxLength);
aX[i].attachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].attachEvent('onpaste', Inputs.MaxLength);
aX[i].attachEvent('onkeypress', Inputs.MaxLength);
aX[i].attachEvent('onkeyup', Inputs.MaxLength);
aX[i].attachEvent('onkeydown', Inputs.MaxLength);
/*
var res = 'zh-cn' == window.navigator.systemLanguage
? '最大长度:'
: 'Maximum length for: ';
if('' == aX[i].title) aX[i].title = res + aX[i].getAttribute('maxlength');
*/
}
}
},
TextBoxMaxLength: function() {
var aX = document.getElementsByTagName('input');
for(var i = 0; i < aX.length; i++) {
var maxLen = aX[i].getAttribute('maxlength');
if('text' == aX[i].type
&& null != maxLen
&& '' != maxLen
&& undefined != maxLen
&& 2147483647 > parseInt(maxLen)
&& '' == aX[i].title) {
aX[i].detachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].detachEvent('onpaste', Inputs.MaxLength);
aX[i].detachEvent('onkeypress', Inputs.MaxLength);
aX[i].detachEvent('onkeyup', Inputs.MaxLength);
aX[i].attachEvent('onbeforepaste', Inputs.MaxLength);
aX[i].attachEvent('onpaste', Inputs.MaxLength);
aX[i].attachEvent('onkeypress', Inputs.MaxLength);
aX[i].attachEvent('onkeyup', Inputs.MaxLength);
/*
var res = 'zh-cn' == window.navigator.systemLanguage
? '最大长度:'
: 'Maximum length for: ';
aX[i].title = res + aX[i].getAttribute('maxlength');
*/
}
}
}
}
Tools.Inputs = Inputs;
window.attachEvent('onload', Tools.Inputs.TextAreaMaxLength);
window.attachEvent('onload', Tools.Inputs.TextBoxMaxLength);
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“TextArea 控件的最大长度问题(js json)”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2024年11月14日
2024年11月14日
- 黑鸭子2010-再度重相逢[首版][WAV+CUE]
- 【原神手游】5.2版本圣遗物优化详情
- 方季惟.1989-一生只爱一次【蓝与白】【WAV+CUE】
- 群星.1997-强力舞曲总动员【金点】【WAV+CUE】
- 盘尼西林.2024-岛与黎明【智慧大狗】【FLAC分轨】
- 刀郎《柔情经典》 2CD[WAV分轨][3.8G]
- 群星2024《民谣精选》原音母版1:1直刻[低速原抓WAV+CUE][1.1G]
- 经典《泰坦尼克号原声大碟》[WAV+DSF+FLAC多版][5.2G]
- 魔兽世界兽王猎输出宏代码是什么 兽王猎翻页输出宏命令代码分享
- 魔兽世界wlk野德一键输出宏是什么 wlk野德一键输出宏介绍
- wlk鸟德一键输出宏是什么 wlk鸟德一键输出宏介绍
- 《明末:渊虚之羽》外网新宣传:有勇气面对障碍吗?
- 视觉盛宴!V社公布《看火人》团队新作水面物理效果演示
- 张艺谋呼吁观众走进影院看电影:对解说短视频很无语
- 车载音乐最强享受 《车载极致女声精选》[WAV分轨][1G]