简体   繁体   English

你如何在jQuery中处理oncut,oncopy和onpaste?

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

The jQuery documentation says the library has built-in support for the following events: blur, focus, load, resize, scroll, unload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, and error. jQuery文档说该库内置支持以下事件:模糊,焦点,加载,调整大小,滚动,卸载,单击,dblclick,mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave,change,select,提交,keydown,按键,键盘和错误。

I need to handle cut, copy, and paste events. 我需要处理剪切,复制和粘贴事件。 How best to do that? 怎么做到最好? FWIW, I only need to worry about WebKit (lucky me!). FWIW,我只需要担心WebKit(幸运的是我!)。

UPDATE: I'm working on a "widget" in a Dashboard-like environment. 更新:我正在类似仪表板的环境中处理“小部件”。 It uses WebKit, so it only really matters (for my purposes) whether these events are supported there, which it looks like they are. 它使用WebKit,因此它(这是为了我的目的)是否真的很重要,这些事件是否在那里被支持,它看起来就像它们一样。

You can add and remove events of any kind by using the .on() and off() methods 您可以使用.on()off()方法添加和删除任何类型的事件

Try this, for instance 比如试试这个

jQuery(document).on('paste', function(e){ alert('pasting!') });

jQuery is actually quite indifferent to whether the event type you assign is supported by the browser, so you can assign arbitrary event types to elements (and general objects) such as: jQuery实际上对浏览器是否支持您分配的事件类型无动于衷,因此您可以将任意事件类型分配给元素(和常规对象),例如:

jQuery('p').on('foobar2000', function(e){ alert(e.type); });

In case of custom event types, you must .trigger() them "manually" in your code, like this: 如果是自定义事件类型,则必须在代码中“手动”使用.trigger() ,如下所示:

jQuery('p').trigger('foobar2000');

Neat eh? 整洁啊?

Furthermore, to work with proprietary/custom DOM events in a cross-browser compatible way, you may need to use/write an "jQuery event plugin" ... example of which may be seen in 此外,要以跨浏览器兼容的方式使用专有/自定义DOM事件,您可能需要使用/编写“jQuery事件插件”...其中的示例可以在 jquery.event.wheel.js jquery.event.wheel.js Brandon Aaron's Mousewheel plugin Brandon Aaron的Mousewheel插件

Various clipboard events are available in Javascript, though support is spotty. Javascript中提供了各种剪贴板事件,但支持很多。 QuicksMode.org has a compatibility grid and test page . QuicksMode.org有一个兼容性网格测试页面 The events are not exposed through jQuery, so you'll either have to extend the library or use native Javascript events. 事件不是通过jQuery公开的,因此您必须扩展库或使用本机Javascript事件。

Mozilla supports an "input" event which I'm having trouble finding useful documentation for. Mozilla支持“输入”事件,我很难找到有用的文档。 At the very least, I know it fires on paste. 至少,我知道它会粘贴。

   this.addEventListener('input',
    function(){//stuff here},
    false
   );

As jQuery 1.7 you can use bind(...) and unbind(...) methods for attaching and removing respectively handlers. 作为jQuery 1.7,您可以使用bind(...)unbind(...)方法分别附加和删除处理程序。

Here are examples align your questuion: 以下示例对齐您的问题:

$('#someElementId').bind('paste', function(){return false;});

- this one will block any attempts to paste from clipboard into element body. - 这个会阻止任何从剪贴板粘贴到元素主体的尝试。 You can use also cut , copy and others as event types (see links bellow) 您还可以使用剪切复制和其他作为事件类型(请参阅下面的链接)

$('#someElementId').bind('copy', function(){return alert('Hey fella! Do not forget about copyrights!');});

So, in other cases, when you want to remove those handlers, you can use unbind() method: 因此,在其他情况下,当您想要删除这些处理程序时,可以使用unbind()方法:

$('#someElementId').unbind('copy');

Here some useful links: 这是一些有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM