Use the onKeyDown and onKeyUp events of js textarea to monitor user key events in real time, and then use our custom statistical function to calculate the number and number of characters that can be entered.
Real-time statistics html
The Code is as follows: |
Copy code |
<Textarea id = "text" cols = "110" rows = "20" dir = "ltr" onKeyDown = "CountStrByte ()" onKeyUp = "CountStrByte ()" Onfocus = "if (this. value = 'high quality and professional answers are easier to follow! ') This. value = '';" onblur = "if (this. value ='') this. value = 'high quality and professional answers are easier to follow! '"> High-quality and professional answers are more likely to be noticed! </Textarea> |
Js Code,Save the following code as a bKjia. c0m. js file.
The Code is as follows: |
Copy code |
Var LastCount = 0; Function CountStrByte () {// byte statistics Var ByteCount = 0; Var StrValue = ID ('text'). value; Var StrLength = StrValue. length; Var MaxValue = 1000; If (LastCount! = StrLength) {// judge here to reduce the number of cycles For (I = 0; I <StrLength; I ++ ){
ByteCount = ByteCount + 1;
If (ByteCount> MaxValue ){
ID ('text'). innerHTML = StrValue. substring (0, I ); Alert ("the comment content cannot exceed 1000 words! ");
ByteCount = MaxValue;
Break; } } ID ('remain'). innerHTML = MaxValue-ByteCount; LastCount = StrLength; } } |