<textarea name="CH" id="CH" cols="16" rows="5"></textarea>想实时显示 输入的行数 能实现吗?

解决方案 »

  1.   

    <script>
    function fot(e){
    if(e.offsetHeight<=24){
    alert(1+"行")
    }
    else{
    alert(((e.offsetHeight - 24)/15+1)+"行")
    }
    }</script>
    <textarea onpropertychange="style.posHeight=scrollHeight+5" style="overflow:hidden" rows="1" cols="20" id="a"></textarea>
    <input name="" type="button" onclick="fot(document.getElementById('a'))" />
      

  2.   

    或者吧,每个字符判断,如果是中文字符算两个,计算总长度再除以每行最大长度
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>    <script language="javascript">
            function showCols(o) {
                document.getElementById("span").innerHTML = o.value.length;
                var length = o.value.length;
                if (length && length > 0) {
                    document.getElementById("span").innerHTML = "共" + (parseInt(length / 16) + (length % 16 > 0 ? 1 : 0)) + "行";
                }
            }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <textarea name="CH" id="CH" cols="16" rows="5" onkeydown="showCols(this);"> </textarea>
            <span id="span"></span>
        </div>
        </form>
    </body>
    </html>
      

  3.   

    textarea里的换行符是/r/n,不能通过换行符计算吗。