关于</textarea>里的字节限制,限制输入小于等于40个字节,超过40个字节键盘不能输入 html5里面input里面有maxlength这个属性,就可以实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">function check(e){ var a=e||window.event; var value=this.value; if(value.length>5){ this.value=value.substring(0,5); } if(value.length==5){ if(a.keyCode!=8){ if(a.preventDefault){ a.preventDefault(); }else{ a.returnValue=false; } } }}window.onload=function(){ var text=document.getElementById("test"); text.onkeydown=check; text.onkeyup=check; text.onkeypress=check;}</script></head><body><textarea id="test"></textarea></body></html>类似这样试试 <html><head></head><body> <textarea id="text"></textarea> <label id="byteCount"></label> </body></html><script> var text = document.getElementById("text"); var byteCount = document.getElementById("byteCount"); var len = 0; text.onkeyup = function(){ len = getByteCount(text.value); if(len > 40){ text.value = getSubStr(text.value,40); } byteCount.innerText = getByteCount(text.value); } //获取字符串字节数 function getByteCount(str){ if (str == null) { return 0; } else { return (str.length + str.replace(/[\u0000-\u00ff]/g, "").length); } } //根据字节数截取字符串 function getSubStr(str,len){ var tmpLen = 0; for (var i = 0; i < str.length; i++) { var c = str.charAt(i); if (/^[\u0000-\u00ff]$/.test(c)){ tmpLen++; }else { tmpLen += 2; } if(tmpLen > len){ break; } } return str.substring(0,i); }</script> <html><head></head><body> <textarea id="text"></textarea> <label id="byteCount"></label> </body></html>$( function(){ $('#text').keypress( function(e){ if ($(this).text().length > 50) $(this).text() = $(this).text().substring(0 ,50); } ) }} 向你推荐个网站,应该能帮助你http://blog.offbeatmammal.com/post/2006/10/26/MaxLength-on-a-Textarea.aspx QQ邮箱收件人跟网易邮箱收件人的选择效果怎么实现? 关于制作论坛问题的请教 如何读懂 大型网站上的 javascript? getelementsbytagname("div")总出错 如果获取iframe的src属性所指向的页面中的html元素的引用? 请教个iframe传值的问题[错误码:5011] 下载百度离线地图,pc端用 关于日期的迷惑 怎样判断用户输入的数字!! JS用变量传递参数的问题 封装页面的js方法 问个JS轮播的效果
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function check(e){
var a=e||window.event;
var value=this.value;
if(value.length>5){
this.value=value.substring(0,5);
}
if(value.length==5){
if(a.keyCode!=8){
if(a.preventDefault){
a.preventDefault();
}else{
a.returnValue=false;
}
}
}
}
window.onload=function(){
var text=document.getElementById("test");
text.onkeydown=check;
text.onkeyup=check;
text.onkeypress=check;
}
</script>
</head><body>
<textarea id="test"></textarea>
</body>
</html>
类似这样试试
<head></head><body>
<textarea id="text"></textarea> <label id="byteCount"></label>
</body>
</html><script> var text = document.getElementById("text");
var byteCount = document.getElementById("byteCount");
var len = 0;
text.onkeyup = function(){
len = getByteCount(text.value);
if(len > 40){
text.value = getSubStr(text.value,40);
}
byteCount.innerText = getByteCount(text.value);
} //获取字符串字节数
function getByteCount(str){
if (str == null) {
return 0;
} else {
return (str.length + str.replace(/[\u0000-\u00ff]/g, "").length);
}
} //根据字节数截取字符串
function getSubStr(str,len){
var tmpLen = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
if (/^[\u0000-\u00ff]$/.test(c)){
tmpLen++;
}else {
tmpLen += 2;
}
if(tmpLen > len){
break;
}
}
return str.substring(0,i); }</script>
<head></head><body>
<textarea id="text"></textarea> <label id="byteCount"></label>
</body>
</html>$(
function(){
$('#text').keypress(
function(e){
if ($(this).text().length > 50) $(this).text() = $(this).text().substring(0 ,50);
}
)
}}
http://blog.offbeatmammal.com/post/2006/10/26/MaxLength-on-a-Textarea.aspx