|          
/* 豆腐制作 都是精品
 http://www.asp888.net 豆腐技术站
 如转载 请保留完整版权信息
 */
 我们知道有的时候必须对文本输入框的输入长度进行限制,我们可以通过很简单的maxlength 对Text 和 Password
 类型的输入框的输入长度进行限制,可是当我们对TextArea 使用maxlength 使用maxlength属性的时候,我们遗憾
 的发现,这个属性在textarea中是不起作用的
 有没有办法呢?答案是肯定的,有!就是使用HTC的技术,什么是HTC??简单的说,htc就是HTML Component,豆腐言语
 表达能力不强,我们看看下面的例子就可以了
 
 test.html:
 <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>
 
 大家注意到 以前很少见过 这样的 用法: style="behavior:url(maxlength.htc)" 我们再看看
 下面的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>
 
 关于htc 的内容讲解在 MSDN 站点上是非常详细的,有兴趣的朋友可以到http://msdn.microsoft.com/workshop/components/htc/reference/htcref.asp
 
 作者:豆腐(原创)
 |