问题描述
如何通过右键单击javascript来处理选中的粘贴?
我试过onpaste事件和所有其他html事件但没有任何效果。
How can I handle the paste selected through right click in javascript?I tried with "onpaste" event and all other html events available but nothing works.
推荐答案
onpaste事件应该在所有现代浏览器中工作( UPD 包括Opera> = 12.10 )。
The onpaste event should work in all modern browsers (UPD Including Opera >= 12.10).
将它绑定在jQuery中,就像这样:
Bind it in jQuery like this:
$('#txt').on('paste', function() {console.log('text pasted!')})
这是一个实例:
在纯JavaScript中它在现代浏览器中看起来像这样吗
In pure JavaScript it would look something like this for modern browsers
elem.addEventListener ("paste", handler, false); // all browsers and IE9+
和旧的IE版本:
elem.attachEvent ("onpaste", handler); // IE<9
您还可以将它与 和其他事件(更改, propertychange , dragdrop 等)以创建相对无懈可击的内容更改跟踪。
You can also combine it with oninput and other events (change, propertychange, dragdrop, etc.) to create a relatively bulletproof tracking of content change.
脚注:
Opera从,对应于建议的12.10 。 Opera的(从15)也应该支持它但我无法测试它,因为仍然没有Linux版本。
Opera supports Clipboard API starting from Presto/2.10.286 which corresponds to 12.10 as suggested here. Blink versions of Opera (starting from 15) should also support it but I am unable to test it as there is still no Linux version.
这篇关于在Javascript中粘贴事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!