• 纯JS控制textarea的maxLength


        不用JQUERY,光是纯JS做的一个控制文本框字数的小功能,代码是用ASP.NET的文本框控件了,自己在chrome,firefox,ie6,8上测试通过了,innerText竟然不能用于firefox。。。郁闷。。。至于网上说的什么粘贴问题之类的那就不管了,那些就留到后台再截取处理就行了。

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="text_area.aspx.cs" Inherits="text_area" %>

    <!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>纯JS限制文本框字数</title>
        
    <script type="text/javascript">
            window.onload 
    = function() {
                document.getElementById(
    "txtContent").onkeyup = function() {
                    
    var len = this.value.length;
                    
    var tmp = 10 - len;
                    
    if (tmp <= 0) {
                        
    this.value = this.value.substring(010);
                        document.getElementById(
    "mes").innerHTML = "您还可以输入 0 个字符";
                    } 
    else {
                        document.getElementById(
    "mes").innerHTML = "您还可以输入 " + tmp + " 个字符";
                    }
                }
            }
        
    </script>

    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <asp:TextBox ID="txtContent" TextMode="MultiLine" runat="server" Height="190px" Width="439px"></asp:TextBox>
        
    <div id="mes">
            您还可以输入 10 个字符
        
    </div>
        
    </form>
    </body>
    </html>
    撸码:复制、粘贴,拿起键盘就是“干”!!!
  • 相关阅读:
    [Bzoj3262]陌上花开(CDQ分治&&树状数组||树套树)
    [洛谷P1501][国家集训队]Tree II(LCT)
    [bzoj2002][Hnoi2010]Bounce 弹飞绵羊(LCT)
    Codeforces Round #683 (Div. 2, by Meet IT) E
    Codeforces Round #683 (Div. 2, by Meet IT) C
    set使用
    Educational Codeforces Round 98 (Rated for Div. 2) D
    Educational Codeforces Round 98 (Rated for Div. 2) B
    arc102a
    树状数组知识点整理二(待)
  • 原文地址:https://www.cnblogs.com/niunan/p/1758253.html
Copyright © 2020-2023  润新知