jQuery绑定到Paste Event,如何获取粘贴的内容

问题描述:

我有一个jquery令牌tagit插件,我想绑定到粘贴事件以正确添加项目。

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:

    .bind("paste", paste_input)

...

function paste_input(e) {
    console.log(e)
    return false;
}

如何获取实际粘贴的内容值?

How can I obtain the actual pasted content value?

有一个onpaste事件可以在现代浏览器中使用。您可以使用 clipboardData 对象上的 getData 函数访问粘贴的数据。

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);
} );

请注意绑定解除绑定自jQuery 3开始不推荐使用。是

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

所有现代浏览器支持剪贴板API

请参阅另外:在Jquery中如何处理粘贴?