textarea標籤提示錄入剩餘字數html
<textarea onkeydown="checkMaxInput(this,300)" onkeyup="checkMaxInput(this,300)" onfocus="checkMaxInput(this,300)" onblur="checkMaxInput(this,300);resetMaxmsg()" style="width:99%;height:190px;" placeholder="這裏寫內容"></textarea>
js方法this
//多行文本輸入框剩餘字數計算 function checkMaxInput(obj, maxLen) { if (obj == null || obj == undefined || obj == "") { return; } if (maxLen == null || maxLen == undefined || maxLen == "") { maxLen = 100; } var strResult; var $obj = $(obj); var newid = $obj.attr("id") + 'msg'; if (obj.value.length > maxLen) { //若是輸入的字數超過了限制 obj.value = obj.value.substring(0, maxLen); //就去掉多餘的字 strResult = '<div style="text-align:right;"><span id="' + newid + '" class=\'Max_msg clearfix\' >剩餘:' + (maxLen - obj.value.length) + '字</span></div>'; //計算並顯示剩餘字數 } else { strResult = '<div style="text-align:right;"><span id="' + newid + '" class=\'Max_msg clearfix\' >剩餘:' + (maxLen - obj.value.length) + '字</span></div>'; //計算並顯示剩餘字數 } var $msg = $("#" + newid); if ($msg.length == 0) { $obj.after(strResult); } else { $msg.html(strResult); } } //清空剩除字數提醒信息 function resetMaxmsg() { $("span.Max_msg").remove(); }