我希望能够限制 textarea 中的字符数。我使用的方法在 Google Chrome 中效果很好,但在 Firefox 中很慢,在 IE 中不起作用。
Javascript:
function len(){
t_v=textarea.value;
if(t_v.length>180){
long_post_container.innerHTML=long_post;
post_button.className=post_button.className.replace('post_it_regular','post_it_disabled');
post_button.disabled=true;
}
else{
long_post_container.innerHTML="";
post_button.className=post_button.className.replace('post_it_disabled','post_it_regular');
post_button.disabled=false;
}
if(t_v.length>186){
t_v=t_v.substring(0,186);
}
}
HTML:
<textarea id="user_post_textarea" name="user_post_textarea" cols="28" rows="1" onkeypress="len();" onkeyup="len();"></textarea>
body 元素底部的 Javascript:
textarea=document.getElementById('user_post_textarea');