テキストエリア内の単語数を制限したいだけで、あまり多くのことを書きたくない場合は、次のように使用できます: < /textarea> または textarea> textarea の onkeyup で単語数を確認する方法: コードをコピー コードは次のとおりです: <br>function DjCheckMaxlength(oInObj) <br>{ <br>var iMaxLen = parseInt(oInObj.getAttribute('maxlength')) ; <br>var iCurLen = oInObj.value.length; <br><br>if ( oInObj.getAttribute && iCurLen > iMaxLen ) <br> oInObj.value = oInObj.value.substring(0, iMaxLen ); <br>} <br>} //@ END OF DjCheckMaxlength() <br> < ;/textarea> o すべてのテキストエリアを自動的にチェックするメソッドを設定します: コードをコピーします コードは次のとおりです: <br>function DjTextArea_AddMaxlengthAttr( ) <br>{ <br>var aX = document.getElementsByTagName('textarea'); <br><br>for ( var iI = 0; aX.length > iI; iI ) <br>{ <br>if ( aX[iI].getAttribute('maxlength') ) <br>{ <br>aX[iI].onkeyup = aX[iI].onchange = DjTextArea_CheckMaxlength; <br>} <br>} <br>} // @ END OF DjTextArea_AddMaxlengthAttr() <br><br>function DjTextArea_CheckMaxlength() <br>{ <br>var iMaxLen = parseInt(this.getAttribute('maxlength') ); <br>var iCurLen = this.value.length; <br><br>if ( this.getAttribute && iCurLen > iMaxLen ) <br>{ <br>this.value = this.value.substring(0, iMaxLen); <br>} <br>} //@ DjTextArea_CheckMaxlength() の終わり <br><br><body onload="DjTextArea_AddMaxlengthAttr() ;"> <br><br><br><br> </div> <br><div class="codetitle">コードをコピー<span><a style="CURSOR: pointer" data="36174" class="copybut" id="copybut36174" onclick="doCopy('code36174')"><u> コードは次のとおりです:</u><div class="codebody" id="code36174"> <br>var 入力 = { <br><br>OnIntegerOnly: function() { <br>var k = System.isIE ?イベント.キーコード: イベント.どれ; <br><br>if(event.ctrlKey ||event.ctrlAlt ||event.shiftKey) { <br>Inputs.DisableInput(event); <br>戻る; <br>} <br><br>if ( ( k 57 ) <br>&& 8 != k <br>&& 9 != k <br>&& 46 != k <br>&& 37 != k <br>&& 39 != k <br>&& 16 != k <br>&& 36 != k <br>&& 35 != k <br>&& 17 != k <br>&& 13 != k <br>&& ( k 105 ) <br>) { <br>Inputs.DisableInput(event); <br>} <br><br>return k; <br>}, <br><br>OnDecimalOnly: function() { <br>var k = System.isIE ?イベント.キーコード: イベント.どれ; <br><br>if(event.ctrlKey ||event.ctrlAlt ||event.shiftKey) { <br>Inputs.DisableInput(event); <br>戻る; <br>} <br><br>if( ( k 57 ) <br>&& 8 != k <br>&& 9 != k <br>&& 46 != k <br>&& 37 != k <br>&& 39 != k <br>&& 16 != k <br>&& 36 != k <br>&& 35 != k <br>&& 17 != k <br>&& 13 != k <br>&& 190 != k <br>&& 110 != k <br>&& ( k 105 ) <br>) { <br>Inputs.DisableInput(event) ; <br>} <br><br>return k; <br>}, <br><br>OnAsciiOnly: function() { <br>var k = System.isIE ?イベント.キーコード: イベント.どれ; <br><br>if ( ( k 122 ) <br>&& ( k 90 ) <br>&& ( k 122 ) 57 ) <br>&& 8 != k <br>&& 9 != k <br>&& 46 != k <br>&& 37 != k <br>&& 39 != k <br>&& 16 != k <br>&& 36 != k <br>&& 35 != k <br>&& 17 != k <br>&& 13 != k <br>&& 189 != k <br>&& 187 != k <br>) { <br>Inputs.DisableInput(event); <br>} <br><br>return k; <br>}, <br><br>DisableInput: function(event) { <br>if(System.isIE) <br>event.returnValue = false; <br>else <br>event.preventDefault(); <br>}, <br><br>AddOnPos: function(obj, charvalue) { <br>//obj 代表要插入文字符の输入框 <br><br>//value 代表要插入文字符 <br> <br>obj.focus(); <br>var r = document.selection.createRange(); <br>var ctr = obj.createTextRange(); <br>変数 i; <br>var s = obj.value; <br><br>//注釈のこのメソッドは実行中の入力フィールド内でのみ使用できます <br><br>//多行入力テキストエリアなし <br>//r.setEndPoint("StartToStart "、Ctrl); <br>//i = r.text.length; <br>//取到光标位置----開始---- <br>var ivalue = "&^asdjfls2FFFF325%$^&"; <br>r.text = ivalue; <br>i = obj.value.indexOf(ivalue); <br>r.moveStart("character", -ivalue.length); <br>r.text = ""; <br>//取到光标位置----終了---- <br>//插入文字 <br>obj.value = s.substr(0,i) charvalue s.substr(i,s.length ); <br>ctr.collapse(true); <br>ctr.moveStart("character", i charvalue.length); <br>ctr.select(); <br>}, <br><br>MaxLength: function() { <br>var _this =event.srcElement; <br>var iMaxLen = parseInt(_this.getAttribute('maxlength')); <br><br>switch(event.type) { <br>case 'beforepaste': { <br>event.returnValue = false; <br>} 休憩; <br><br>case 'paste': { <br>var iTxtLen = _this.value.lenUTF8(); <br>var txtCbd = window.clipboardData.getData("テキスト"); <br>var iCbdLen = txtCbd.lenUTF8(); <br>if((iMaxLen - iTxtLen) var iSurplusLen = iMaxLen - iTxtLen; <br>var subStr = txtCbd.substr(0, iSurplusLen); <br>var insertStr = ''; <br>for(var i = 0; i <substr.length i>var chr = subStr.substr(i, 1); <br>if(iSurplusLen >= chr.lenUTF8()) { <br>insertStr = chr; <br>iSurplusLen -= chr.lenUTF8(); <br>} <br>} <br><br>Inputs.AddOnPos(_this, insertStr); <br>//_this.style.backgroundColor = 'red'; <br>//System.setTimeout(Inputs.ColorTips, 500, _this); <br>event.returnValue = false; <br>event.srcElement.style.imeMode = '無効'; <br>} else { <br>event.returnValue = true; <br>event.srcElement.style.imeMode = 'auto'; <br>} <br>} 休憩; <br><br>case 'keypress': { <br>var iTxtLen = _this.value.lenUTF8(); <br>if(iMaxLen while(_this.value.lenUTF8() > iMaxLen) { <br>_this.value = _this.value.substr(0, _this.value.length - 1); <br>} <br>Inputs.DisableInput(event); <br>if('無効' !=event.srcElement.style.imeMode) { <br>event.srcElement.style.imeMode = '無効'; <br>} <br>} <br>else { <br>if('auto' !=event.srcElement.style.imeMode) { <br>event.srcElement.style.imeMode = 'auto'; <br>} <br>} <br>} 休憩; <br><br>case 'keyup': { <br>var k = System.isIE ?イベント.キーコード: イベント.どれ; <br><br>if (8 != k <br>&& 9 != k <br>&& 46 != k <br>&& 37 != k <br>&& 39 != k <br>&& 16 ! = k <br>&& 36 != k <br>&& 35 != k <br>&& 17 != k <br>&& 13 != k <br>&& 189 != k <br>&& 187 != k <br>) { <br>if(_this.getAttribute) { <br>_this.fireEvent('onkeypress'); <br>} <br>} <br>} 休憩; <br><br>case 'keydown': { <br>var k = System.isIE ?イベント.キーコード: イベント.どれ; <br><br>if (8 != k <br>&& 9 != k <br>&& 46 != k <br>&& 37 != k <br>&& 39 != k <br>&& 16 ! = k <br>&& 36 != k <br>&& 35 != k <br>&& 17 != k <br>&& 13 != k <br>&& 189 != k <br>&& 187 != k <br>) { <br>var iTxtLen = _this.value.lenUTF8(); <br>if(iMaxLen Inputs.DisableInput(event); <br>} <br>} <br>} 休憩; <br>} <br>}, <br><br>TextAreaMaxLength: function() { <br><br>var aX = document.getElementsByTagName('textarea'); <br>for(var i = 0; i <ax.length i>if(aX[i].getAttribute('maxlength')) { <br>aX[i].detachEvent('onbeforepaste '、Inputs.MaxLength); <br>aX[i].detachEvent('onpaste', Inputs.MaxLength); <br>aX[i].detachEvent('onkeypress', Inputs.MaxLength); <br>aX[i].detachEvent('onkeyup', Inputs.MaxLength); <br>aX[i].detachEvent('onkeydown', Inputs.MaxLength); <br><br>aX[i].attachEvent('onbeforepaste', Inputs.MaxLength); <br>aX[i].attachEvent('onpaste', Inputs.MaxLength); <br>aX[i].attachEvent('onkeypress', Inputs.MaxLength); <br>aX[i].attachEvent('onkeyup', Inputs.MaxLength); <br>aX[i].attachEvent('onkeydown', Inputs.MaxLength); <br>/* <br>var res = 'zh-cn' == window.navigator.systemLanguage <br>? '最大長さ:' <br>: '最大長: '; <br><br>if('' == aX[i].title) aX[i].title = res aX[i].getAttribute('maxlength'); <br>*/ <br>} <br>} <br>}, <br><br>TextBoxMaxLength: function() { <br>var aX = document.getElementsByTagName('input'); <br>for(var i = 0; i <ax.length i>var maxLen = aX[i].getAttribute('maxlength'); <br>if('text' == aX[i].type <br>&& null != maxLen <br>&& '' != maxLen <br>&& 未定義 != maxLen <br>&& 2147483647 > parseInt( maxLen) <br>&& '' == aX[i].title) { <br><br>aX[i].detachEvent('onbeforepaste', Inputs.MaxLength); <br>aX[i].detachEvent('onpaste', Inputs.MaxLength); <br>aX[i].detachEvent('onkeypress', Inputs.MaxLength); <br>aX[i].detachEvent('onkeyup', Inputs.MaxLength); <br><br>aX[i].attachEvent('onbeforepaste', Inputs.MaxLength); <br>aX[i].attachEvent('onpaste', Inputs.MaxLength); <br>aX[i].attachEvent('onkeypress', Inputs.MaxLength); <br>aX[i].attachEvent('onkeyup', Inputs.MaxLength); <br>/* <br>var res = 'zh-cn' == window.navigator.systemLanguage <br>? '最大長さ:' <br>: '最大長: '; <br><br>aX[i].title = res aX[i].getAttribute('maxlength'); <br>*/ <br>} <br>} <br>} <br>} <br><br>Tools.Inputs = 入力; <br><br>window.attachEvent('onload', Tools.Inputs.TextAreaMaxLength); <br>window.attachEvent('onload', Tools.Inputs.TextBoxMaxLength);<br></ax.length></ax.length></substr.length> </div></a></span> </div>