/** * 监听input的数据数字 * @param count 限制数量 * @param contentId input/textarea 输入框的id * @param limitId 展示数字的span */ function initLimit(count, contentId, limitId) { var lim = new limit(); lim.txtNote = document.getElementById(contentId); lim.txtLimit = document.getElementById(limitId); lim.limitCount = count; lim.init(); function limit() { var txtNote;//文本框 var txtLimit;//提示字数的input var limitCount;//限制的字数 var txtLength;//到达限制时,字符串的长度 this.init = function () { txtNote = this.txtNote; txtLimit = this.txtLimit; limitCount = this.limitCount; txtNote.oninput = function () { wordsLimit() }; txtNote.oninput = function () { wordsLimit() }; txtLimit.innerText = limitCount; }; function wordsLimit() { var noteCount = txtNote.value.length; var InPut = document.getElementById(contentId).value.length; if (InPut < 1) { //document.getElementById("stay").style.display="none"; } if (InPut >= 1) { //document.getElementById("stay").style.display="inline"; //document.getElementById("stay").style.color="green"; } if (InPut > 70) { //document.getElementById("stay").style.color="red"; } if (noteCount > limitCount) { txtNote.value = txtNote.value.substring(0, limitCount); txtLimit.innerText = 0; } else { txtLimit.innerText = limitCount - noteCount; } txtLength = txtNote.value.length;//记录每次输入后的长度 } } }