关闭

html中利用javascript实现文本框字数的动态计算

343人阅读 评论(0) 收藏 举报

将下列代码存为一个htm文件即可.

< script language="javascript">
var cmccCount = 60; unionCount = 60; phsCount = 30; cncCount = -1;
function showcount(obj)
{
 var len = obj.value.length;
 document.all.wordCount.innerText = '当前字符数:'+len;
 if(cmccCount > 0)
  document.all.cmccSeg.innerText = '移动当前分段:'+ Math.floor(len / cmccCount + 1);
 else
  document.all.cmccSeg.innerText = '移动当前分段:-1';
 if(unionCount > 0)
  document.all.unionSeg.innerText = '联通当前分段:'+ Math.floor(len / unionCount + 1);
 else
  document.all.unionSeg.innerText = '联通当前分段:-1';
 if(phsCount > 0)
  document.all.phsSeg.innerText = '电信当前分段:'+ Math.floor(len / phsCount + 1);
 else
  document.all.phsSeg.innerText = '电信当前分段:-1';
 if(cncCount > 0)
  document.all.cncSeg.innerText = '网通当前分段:'+ Math.floor(len / cncCount + 1);
 else
  document.all.cncSeg.innerText = '网通当前分段:-1';
}
function printResult(obj){ obj.document.execCommand('Print',false); }
</script>

<form>
    this is title not to be printed.
    <table id="tbl">
        <tr>
            <td>
                <textarea id="tt" onkeyup="showcount(this)"></textarea>
                <input type="text" id="cc"></input>
                <span id="wordCount"></span>
                <br />
                <span id="cmccSeg"></span>
                <br />
                <span id="unionSeg"></span>
                <br />
                <span id="phsSeg"></span>
                <br />
                <span id="cncSeg"></span>
                <input type="button" onclick="printResult(tbl);" value="print" />
            </td>
        </tr>
    </table>
    and this too.
</form>
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:217603次
    • 积分:2914
    • 等级:
    • 排名:第12159名
    • 原创:15篇
    • 转载:274篇
    • 译文:0篇
    • 评论:20条
    最新评论