代码之家  ›  专栏  ›  技术社区  ›  Georg Schölly Crazy Developer

如何修改粘贴的文本?

  •  2
  • Georg Schölly Crazy Developer  · 技术社区  · 14 年前

    是否可以截取和修改粘贴到文本区域的文本?

    如果无法拦截,粘贴后是否可以修改?(不修改文本区域中已存在的文本。)

    3 回复  |  直到 12 年前
        1
  •  2
  •   Carlos    14 年前

    使用jQuery:

        jQuery(function($){
          $('#your_element').bind('paste', function(event){
            event.preventDefault();
            var clipboardData = event.originalEvent.clipboardData.getData('text/plain');
            console.log(clipboardData);
          }); 
         }      
        });
    

    http://intridea.com/2007/12/16/faking-onpaste-in-firefox?blog=company

        2
  •  1
  •   aularon    14 年前

    可能拦截 keypress 是的,想知道什么时候 CTRL键 C级 按下,缓存当前文本,然后 CTRL键 C级 keyup ,对照缓存的值检查当前值,通过简单的文本处理,您可以知道新文本,并对其执行任何操作,并相应地进行更新。

        3
  •  0
  •   user396404    14 年前

    <script language="javascript">
    
    function testfunction()
    {
    
     // This function will execute whenever the content of 
    
    }
    </script>
    
    <textarea onkeyup="testfunction()" onkeydown="testfunction()"></textarea>
    

    如果您想监视textarea的任何更改,下面的代码可以做到这一点。它每十分之一秒检查一次textarea的值是否有任何更新。

    <textarea id="testfield"></textarea>
    
    <script language="javascript">
    
    var textarea = document.getElementById('testfield');
    var textval = '';
    
    function monitor()
    {
    
     if(textval != textarea.value)
     {
    
      textval = textarea.value;
      testfunction();
    
     }
    
     setTimeout('monitor()', 100);
    
    }
    
    function testfunction()
    {
    
     // This function will get executed whenever the value of the text area is altered (at most within 1/10th of a second)
    
    }
    
    monitor();
    </script>