jQuery: How to get the data copied to clipboard using on('copy', ...)

时间:2016-07-11 20:12:53

标签: javascript jquery

I'm using the following event that fires when a user copies data from the page to the clipboard. But, I can't seem to get the content of the copied data. Is it possible?

$(document).on('copy', function(e){ 
    console.log(e);
});

I've used Chrome's inspector to inspect the object e. There's a property called origionalEvent that has a property origionalText. This only seems to contain the first line of the copied text.

Maybe I'm missing something obvious but I can't seem to figure out how to get the data copied.

Is it possible?

2 个答案:

答案 0 :(得分:1)

If content has been selected to copy, this will get you the selected content at the time of the copy event:

$(document).on('copy', function(e){ 
    console.log(window.getSelection().toString());
    });

答案 1 :(得分:1)

Try it :

<html>
    <head></head>
    <body>
        <p>This is test</p>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            $("p").on("copy",function(){
                var sel = document.getSelection();
                alert(sel);
            })
        </script>
    </body>
</html>