zoukankan      html  css  js  c++  java
  • TextArea设置MaxLength的代码(未测试在不同浏览器下的兼容性)

    function SetTextAreaMaxLength(controlId,length)
    {
        // JScript File for TextArea
        // Keep user from entering more than maxLength characters
        function doKeypress(control,length){
            maxLength = length;
            value = control.value;
            if(maxLength && value.length > maxLength-1){
                event.returnValue = false;
                maxLength = parseInt(maxLength);
            }
        }
        // Cancel default behavior
        function doBeforePaste(control,length){
            maxLength = length;
            if(maxLength)
            {
                event.returnValue = false;
            }
        }
        // Cancel default behavior and create a new paste routine
        function doPaste(control,length){
            maxLength = length;
            value = control.value;
            if(maxLength){
                event.returnValue = false;
                maxLength = parseInt(maxLength);
                var oTR = control.document.selection.createRange();
                var iInsertLength = maxLength - value.length + oTR.text.length;
                var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
                oTR.text = sData;
            }
        }
        
        function doDragenter(control,length)
        {
            maxLength = length;
            value = control.value;
            if(maxLength){
                event.returnValue = false;
            }   
        }
    
        
        function addEvent(elm, evType, fn, useCapture)
        { 
            if (elm.addEventListener)
            { 
                elm.addEventListener(evType, fn, useCapture); 
                return true; 
            } 
            else if (elm.attachEvent) 
            { 
                var r = elm.attachEvent('on' + evType, fn); 
                return r; 
            } 
            else { 
                elm['on' + evType] = fn; 
            } 
        }
    
    
        function AttacheventTextAreaBeforePaste(obj,length)
        {
            return function()
            {
                doBeforePaste(obj,length)
            }
        }
        
        function AttacheventTextAreaPaste(obj,length)
        {
            return function()
            {
                doPaste(obj,length)
            }
        }
        
        function AttacheventTextAreaKeyPress(obj,length)
        {
            return function()
            {
                doKeypress(obj,length)
            }
            
        }
        
        function AttacheventTextAreaDragEnter(obj,length)
        {
            return function()
            {
                doDragenter(obj,length);
            }
        }
        
        
        var obj = document.getElementById(controlId);
        
        addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null);
        addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null);
        addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null);
        addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null); 
    }
    

      

  • 相关阅读:
    [转 scrum] Scrum的三个物件
    [转 scrum] 敏捷误解之无计划
    [转 scrum] Scrum三个角色及其职责介绍
    [转 scrum] Scrum的三个物件
    [转 scrum] Scrum三个角色及其职责介绍
    [转 scrum] Scrum术语字典
    浅尝异步IO
    LINUX内核内存屏障
    在C/C++程序里打印调用栈信息
    Ubuntu10.04下Linux内核编译的完整步骤
  • 原文地址:https://www.cnblogs.com/briny/p/3463517.html
Copyright © 2011-2022 走看看