/* 豆腐制作 都是精品 http://www.asp888.net 豆腐技術(shù)站 如轉(zhuǎn)載 請(qǐng)保留完整版權(quán)信息 */ 我們知道有的時(shí)候必須對(duì)文本輸入框的輸入長(zhǎng)度進(jìn)行限制,我們可以通過很簡(jiǎn)單的maxlength 對(duì)Text 和 Password 類型的輸入框的輸入長(zhǎng)度進(jìn)行限制,可是當(dāng)我們對(duì)TextArea 使用maxlength 使用maxlength屬性的時(shí)候,我們遺憾 的發(fā)現(xiàn),這個(gè)屬性在textarea中是不起作用的 有沒有辦法呢?答案是肯定的,有!就是使用HTC的技術(shù),什么是HTC??簡(jiǎn)單的說,htc就是HTML Component,豆腐言語 表達(dá)能力不強(qiáng),我們看看下面的例子就可以了
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)" 我們?cè)倏纯? 下面的htc 的內(nèi)容
<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>
關(guān)于htc 的內(nèi)容講解在 MSDN 站點(diǎn)上是非常詳細(xì)的,有興趣的朋友可以到http://msdn.microsoft.com/workshop/components/htc/reference/htcref.asp
作者:豆腐(原創(chuàng))
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!