Explain the jQuery code: $('.maxlength').after("<span></span>").next().hide().end().keypress
Here is my code.. but I am unable to understand this code.
$('.maxlength')
.after("<span></span>")
.next()
.hide()
.end()
.keypress(function(e) {
var cur开发者_运维知识库rent = $(this).val().length;
if (current >= 130) {
if (e.which != 0 && e.which != 8) {
e.preventDefault();
}
}
$(this).next().show().text(130 - current);
});
$('.maxlength') // select all items with class 'maxlength'
.after("<span></span>") // insert a span after
.next() // move to the span
.hide() // hide the span
.end() // go back to originally selected element
.keypress(function(e) { // add a keypress event handler function
var current = $(this).val().length; // get the length of the input value (a string)
if (current >= 130) { //if it's long
if (e.which != 0 && e.which != 8) { // and if certain keys weren't pressed (delete?)
e.preventDefault(); // don't do what those keys would normally do - i.e. ignore the keypress
}
}
$(this).next().show().text(130 - current); // show the remaining chars in the newly added span
});
...so basically this code makes a text area have a character limit of 130 characters and shows you how many more you're allowed to type.
精彩评论