87,907
社区成员
发帖
与我相关
我的任务
分享
<head>
<title>--!</title>
<script type="text/javascript">
/*
*当textarea获得焦点,显示当前输入字数
*当textarea失去焦点,隐藏
*/
//获得焦点,显示
function show_count(){
var mydiv = document.getElementById("d");
var max = 90;
var count = document.getElementById("area1").value.length;
if(mydiv.value ==""||mydiv.value ==undefined)
mydiv.innerHtml =count +"/" + max;
mydiv.style.display = "block";
}
//失去焦点,隐藏
function hidden_count(){
document.getElementById("d").style.display = "none";
}
//textarea域改变,改变div文本
function word_count(){
var mydiv = document.getElementById("d");
var max = 90;
var count = document.getElementById("area1").value.length;
if(count<max)
mydiv.innerHtml = count +"/" + max;
else
document.getElementsByTagName("textarea")[0].value = document.getElementsByTagName("textarea")[0].value.substr(0,max);
}
function load(){ var text = document.getElementsByTagName("textarea")[0];
//请空
text.value = "";
text.focus();
}
</script>
<style type="text/css">
#d{
display: none;
position: absolute;
width: 50px;
height: 14px;
color: black;
background: yellow;
}
</style>
</head>
<body>
<form>
<textarea onfocus="show_count()" onblur="hidden_count()"
onload="load()"
onpropertychange="word_count()" cols="60" rows="8" id="area1">123</textarea>
<div id="d">mxl</div>
</form>
</body>
</html>