`
webcode
  • 浏览: 5911116 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

限制文本框输入字数,以百分比显示的特效

阅读更多
http://www.corange.cn//uploadfiles/20080717_57054.jpg

<styletype="text/css">
.progress{
width:1px;
height:14px;
color:white;
font-size:12px;
overflow:hidden;
background-color:navy;
padding-left:5px;
}
</style>
<scripttype="text/JavaScript">
functiontextCounter(field,counter,maxlimit,linecounter){
//textwidth//
varfieldWidth=parseInt(field.offsetWidth);
varcharcnt=field.value.length;
//trimtheextratext
if(charcnt>maxlimit){
field.value=field.value.substring(0,maxlimit);
}
else{
//progressbarpercentage
varpercentage=parseInt(100-((maxlimit-charcnt)*100)/maxlimit);
document.getElementById(counter).style.width=parseInt((fieldWidth*percentage)/100)+"px";
document.getElementById(counter).innerHTML="已输:"+percentage+"%"
//colorcorrectiononstylefromCCFFF->CC0000
setcolor(document.getElementById(counter),percentage,"background-color");
}
}
functionsetcolor(obj,percentage,prop){
obj.style[prop]="rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}
</script>
<p>限制:120字节</P>
<form>
<textarearows="5"cols="40"name="maxcharfield"id="maxcharfield"
onKeyDown="textCounter(this,'progressbar1',120)"
onKeyUp="textCounter(this,'progressbar1',120)"
onPaste="textCounter(this,'progressbar1',120)"
onFocus="textCounter(this,'progressbar1',120)"></textarea><br/>
<divid="progressbar1"class="progress"></div>
<script>textCounter(document.getElementById("maxcharfield"),"progressbar1",120)</script>
</form>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics