不用JQUERY,光是纯JS做的一个控制文本框字数的小功能,代码是用ASP.NET的文本框控件了,自己在chrome,firefox,ie6,8上测试通过了,innerText竟然不能用于firefox。。。郁闷。。。至于网上说的什么粘贴问题之类的那就不管了,那些就留到后台再截取处理就行了。
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
text_area.aspx.cs
"
Inherits
=
"
text_area
"
%>
<! 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 runat ="server" >
< title > 纯JS限制文本框字数 </ title >
< script type ="text/javascript" >
window.onload = function () {
document.getElementById( " txtContent " ).onkeyup = function () {
var len = this .value.length;
var tmp = 10 - len;
if (tmp <= 0 ) {
this .value = this .value.substring( 0 , 10 );
document.getElementById( " mes " ).innerHTML = " 您还可以输入 0 个字符 " ;
} else {
document.getElementById( " mes " ).innerHTML = " 您还可以输入 " + tmp + " 个字符 " ;
}
}
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:TextBox ID ="txtContent" TextMode ="MultiLine" runat ="server" Height ="190px" Width ="439px" ></ asp:TextBox >
< div id ="mes" >
您还可以输入 10 个字符
</ div >
</ form >
</ body >
</ html >
<! 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 runat ="server" >
< title > 纯JS限制文本框字数 </ title >
< script type ="text/javascript" >
window.onload = function () {
document.getElementById( " txtContent " ).onkeyup = function () {
var len = this .value.length;
var tmp = 10 - len;
if (tmp <= 0 ) {
this .value = this .value.substring( 0 , 10 );
document.getElementById( " mes " ).innerHTML = " 您还可以输入 0 个字符 " ;
} else {
document.getElementById( " mes " ).innerHTML = " 您还可以输入 " + tmp + " 个字符 " ;
}
}
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:TextBox ID ="txtContent" TextMode ="MultiLine" runat ="server" Height ="190px" Width ="439px" ></ asp:TextBox >
< div id ="mes" >
您还可以输入 10 个字符
</ div >
</ form >
</ body >
</ html >