可以模拟一个,不够好:失去焦点的时候进行判断
<textarea name=show rows=10 cols=80 style="maxlength:10" onblur=check(this)></textarea>
<script>
String.prototype.getLength = function(){
//得到字符串的真实长度
var l=this.length;
var n=l
for (var i=0;i<l;i++){
if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++
}
return n
}function check(obj){
var max=obj.style.maxlength;
if(obj.value.getLength()>max) {
alert("最大允许长度是"+max);
obj.focus();
obj.select();
}
}
</script>
<textarea name=show rows=10 cols=80 style="maxlength:10" onblur=check(this)></textarea>
<script>
String.prototype.getLength = function(){
//得到字符串的真实长度
var l=this.length;
var n=l
for (var i=0;i<l;i++){
if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++
}
return n
}function check(obj){
var max=obj.style.maxlength;
if(obj.value.getLength()>max) {
alert("最大允许长度是"+max);
obj.focus();
obj.select();
}
}
</script>
<input maxlength=10><p>
<textarea name=show rows=10 cols=80 style="maxlength:10" onkeydown='return check(this)' onpaste='return checkPaste(this)'></textarea>
<script>
String.prototype.getLength = function(){
//得到字符串的真实长度
var l=this.length;
var n=l
for (var i=0;i<l;i++){
if (this.charCodeAt(i)<0||this.charCodeAt(i)>255) n++
}
return n
}function check(obj){
var nKeyCode=event.keyCode;
if(nKeyCode==8 || nKeyCode==46) return true;
var rng=document.selection.createRange();
var nLeft=obj.style.maxlength-obj.value.getLength()+rng.text.getLength();
if(nLeft<=0) return false;
return true;
}
function checkPaste(obj){
var rng=document.selection.createRange();
var nLeft=obj.style.maxlength-obj.value.getLength()+rng.text.getLength();
if(nLeft<=0) return false;
var strCopy=window.clipboardData.getData("Text");
var i=nLeft;
while(strCopy.getLength()>nLeft && i>0){
strCopy=strCopy.substring(0,i--);
}
if(strCopy.getLength()>nLeft) return false;
rng.text=strCopy;
return false;
}
</script>
<form method="POST">
<p><input type="text" size="30" maxlength="50" name="T1">
<textarea name="S1" rows="4" cols="30" maxlength="50" style="behavior:url(maxlength.htc)"></textarea>
</form>maxlength.htc:<PUBLIC:COMPONENT id="bhvMaxlength" urn="maf:Maxlength">
<PUBLIC:PROPERTY name="maxLength" />
<PUBLIC:ATTACH event="onkeypress" handler="doKeypress" />
<PUBLIC:ATTACH event="onbeforepaste" handler="doBeforePaste" />
<PUBLIC:ATTACH event="onpaste" handler="doPaste" /><SCRIPT language="JScript">
// Keep user from entering more than maxLength characters
function doKeypress(){
if(!isNaN(maxLength)){
maxLength = parseInt(maxLength);
var oTR = element.document.selection.createRange();
// Allow user to type character if at least one character is selected
if(oTR.text.length >= 1)
event.returnValue = true;
else if(value.length > maxLength-1)
event.returnValue = false;
}
}
// Cancel default behavior
function doBeforePaste(){
if(!isNaN(maxLength))
event.returnValue = false;
}
// Cancel default behavior and create a new paste routine
function doPaste(){
if(!isNaN(maxLength)){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = element.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}
</SCRIPT></PUBLIC:COMPONENT>
怎么运行?