function checkLength(){ var curLength = $a("roomDesc").value.length; if (curLength>100){ $a("roomDesc").value = $a("roomDesc").value.substr(0,100); curLength = 100; } $a("noticeSpan").innerHTML = 100-$a("roomDesc").value.length; } function $a(id){ return document.getElementById(id); } window.onload = function(){ if(/msie/i.test(navigator.userAgent)){ //ie 浏览器 $a("roomDesc").onpropertychange = checkLength; } else { $a("roomDesc").addEventListener("input",checkLength,false); } }
html 代码
还可以输入<span id="noticeSpan">100</span>字