Js
use JS to calculate the remaining available words
The input box allows the word range to the user does not seem to be visible, is a very abstract thing, JS can provide users with a very intuitive number, so that users control the use of the word.
* This effect involves a bit of HTML DOM knowledge and JS knowledge
Effect
4525364564645645645646
text Maximum length: 250. Still left: 228.
Code explanation
First look at the HTML code:
<textarea name= "description" onkeyup= "checklength (this);" ></textarea>
<BR/><small> Text Maximum length: 250. Still left: <span id= "Chleft" >250</span>.</small>
You can see that onkeyup is the event that is triggered when the user leaves the keyboard, and the parameter passed is this (that is, the current document area)
Then combine the JS code to see:
<script type= "Text/javascript" >
function Checklength (which) {
var maxchars = 250;
if (Which.value.length > Maxchars)
Which.value = which.value.substring (0,maxchars);
var curr = maxchars-which.value.length;
document.getElementById ("Chleft"). InnerHTML = Curr.tostring ();
}
</script>
The function first assigns a value to the Maxchars variable (the maximum number of characters available in the input area, note that the variable is a value that can be computed)
The length of the character entered in the textarea is then obtained from the parameter and compared to the maximum length specified above.
When you enter a character length that exceeds a range, using substring to force its length (0,maxchars) means that the input range is 0 characters to the maxchars (variable) character.
The function of var Curr = Maxchars-which.value.length is to figure out how many characters can be used to save the value in Curr.
Finally, the getElementById is positioned to the object with ID Chleft (span in that HTML) and the value in Curr is converted to a string by the ToString method, which is fed back into the span label.