在Javascript中粘贴事件 [英] Paste event in Javascript

查看:101
本文介绍了在Javascript中粘贴事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过右键单击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 1 )。

The onpaste event should work in all modern browsers (UPD Including Opera >= 12.101).

将它绑定在jQuery中,就像这样:

Bind it in jQuery like this:

$('#txt').on('paste', function() {console.log('text pasted!')})​

这是一个实例:
http://jsfiddle.net/7N6Xq/

在纯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

您还可以将它与 oninput 和其他事件(更改 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.

脚注:

1 Opera从 Presto / 2.10.286 ,对应于建议的12.10 此处。 Opera的 Blink版本(从15)也应该支持它但我无法测试它,因为仍然没有Linux版本。

1 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中粘贴事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆