为textarea 增加 maxlength 属性
為textarea增加maxlength屬性
o 如果只是單純地想限制 textarea 中的字數,不想寫太多的話,可用: <textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea> 或 <textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea> o 在 textarea 的 onkeyup 中檢查字數之方法: ..... <script language="javascript" type="text/javascript"> function DjCheckMaxlength(oInObj) { var iMaxLen = parseInt(oInObj.getAttribute('maxlength')); var iCurLen = oInObj.value.length; if ( oInObj.getAttribute && iCurLen > iMaxLen ) { oInObj.value = oInObj.value.substring(0, iMaxLen); } } //@ END OF DjCheckMaxlength() </script> ..... <textarea maxlength="80" onkeyup="return DjCheckMaxlength(this);"></textarea> ..... o 設定好自動去檢查所有的 textarea 之方法: ..... <head> <script language="javascript" type="text/javascript"> function DjTextArea_AddMaxlengthAttr() { var aX = document.getElementsByTagName('textarea'); for ( var iI = 0; aX.length > iI; iI++ ) { if ( aX[iI].getAttribute('maxlength') ) { aX[iI].onkeyup = aX[iI].onchange = DjTextArea_CheckMaxlength; } } } //@ END OF DjTextArea_AddMaxlengthAttr() function DjTextArea_CheckMaxlength() { var iMaxLen = parseInt(this.getAttribute('maxlength')); var iCurLen = this.value.length; if ( this.getAttribute && iCurLen > iMaxLen ) { this.value = this.value.substring(0, iMaxLen); } } //@ END OF DjTextArea_CheckMaxlength() </script> </head> ..... <body onload="DjTextArea_AddMaxlengthAttr();"> .....
相关推荐
zengni 2020-04-16
Vipwxs 2018-12-19
javaer 2008-09-02
jscjxysx 2019-07-01
冯二爷 2019-07-01
Donutsapps 2019-06-29
无情 2018-12-19
87281248 2017-09-28
原為簡單 2017-09-28
web开发 2017-01-16
勤能补拙孰能生巧 2019-06-28
wndong 2015-12-14
shishaoe 2015-04-10
csstpeixun 2015-04-02
xiaocaibai 2014-12-19
realitycss 2014-05-05
DreamLee 2013-12-13
切慕溪水 2013-07-11
lkj0 2013-05-24