jQuery bind to Paste Event, how to get the content of the paste

后端 未结 9 1041
温柔的废话
温柔的废话 2020-11-28 06:08

I have a jquery token tagit plugin and I want to bind to the paste event to add items correctly.

I\'m able to bind to the paste event like so:

    .b         


        
相关标签:
9条回答
  • 2020-11-28 06:47

    I recently needed to accomplish something similar to this. I used the following design to access the paste element and value. jsFiddle demo

    $('body').on('paste', 'input, textarea', function (e)
    {
        setTimeout(function ()
        {
            //currentTarget added in jQuery 1.3
            alert($(e.currentTarget).val());
            //do stuff
        },0);
    });
    
    0 讨论(0)
  • 2020-11-28 06:51

    You could compare the original value of the field and the changed value of the field and deduct the difference as the pasted value. This catches the pasted text correctly even if there is existing text in the field.

    http://jsfiddle.net/6b7sK/

    function text_diff(first, second) {
        var start = 0;
        while (start < first.length && first[start] == second[start]) {
            ++start;
        }
        var end = 0;
        while (first.length - end > start && first[first.length - end - 1] == second[second.length - end - 1]) {
            ++end;
        }
        end = second.length - end;
        return second.substr(start, end - start);
    }
    $('textarea').bind('paste', function () {
        var self = $(this);
        var orig = self.val();
        setTimeout(function () {
            var pasted = text_diff(orig, $(self).val());
            console.log(pasted);
        });
    });
    
    0 讨论(0)
  • 2020-11-28 06:52

    How about this: http://jsfiddle.net/5bNx4/

    Please use .on if you are using jq1.7 et al.

    Behaviour: When you type anything or paste anything on the 1st textarea the teaxtarea below captures the cahnge.

    Rest I hope it helps the cause. :)

    Helpful link =>

    How do you handle oncut, oncopy, and onpaste in jQuery?

    Catch paste input

    code

    $(document).ready(function() {
        var $editor    = $('#editor');
        var $clipboard = $('<textarea />').insertAfter($editor);
    
        if(!document.execCommand('StyleWithCSS', false, false)) {
            document.execCommand('UseCSS', false, true);
        }
    
        $editor.on('paste, keydown', function() {
            var $self = $(this);            
            setTimeout(function(){ 
                var $content = $self.html();             
                $clipboard.val($content);
            },100);
         });
    });
    
    0 讨论(0)
  • 2020-11-28 06:56

    There is an onpaste event that works in modern day browsers. You can access the pasted data using the getData function on the clipboardData object.

    $("#textareaid").bind("paste", function(e){
        // access the clipboard using the api
        var pastedData = e.originalEvent.clipboardData.getData('text');
        alert(pastedData);
    } );
    

    Note that bind and unbind are deprecated as of jQuery 3. The preferred call is to on.

    All modern day browsers support the Clipboard API.

    See also: In Jquery How to handle paste?

    0 讨论(0)
  • 2020-11-28 06:57
    $(document).ready(function() {
        $("#editor").bind('paste', function (e){
            $(e.target).keyup(getInput);
        });
    
        function getInput(e){
            var inputText = $(e.target).html(); /*$(e.target).val();*/
            alert(inputText);
            $(e.target).unbind('keyup');
        }
    });
    
    0 讨论(0)
  • 2020-11-28 07:01

    Another approach: That input event will catch also the paste event.

    $('textarea').bind('input', function () {
        setTimeout(function () { 
            console.log('input event handled including paste event');
        }, 0);
    });
    
    0 讨论(0)
提交回复
热议问题