copy
当用户通过浏览器 UI(例如,使用 Ctrl/⌘+C 键盘快捷方式或从菜单中选择“复制”)启动复制操作并响应允许的document.execCommand('copy')
调用时触发copy
事件。
基本信息
- Specification
- Interface
ClipboardEvent
- Bubbles
-
Yes
- Cancelable
-
Yes
- Target
-
Element
:获得焦点的元素(如contentEditable
内容能编辑或者可以选中的元素),或<body>
。 - Default Action
-
见下文。
调用setData(format, data)
可以修改ClipboardEvent.clipboardData
事件的默认行为:
document.addEventListener('copy', function(e){
e.clipboardData.setData('text/plain', 'Hello, world!');
e.clipboardData.setData('text/html', '<b>Hello, world!</b>');
e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
});
不能使用clipboardData.getData()
在事件处理函数中获取剪切板数据。
事件的默认行为与事件的来源和事件处理函数相关:
- synthetic (en-US) copy 事件没有默认行为,除非:
- 如果默认事件没有取消,就复制到选区(如果有选中内容)到剪切板;
- 如果取消了默认事件,但是调用了
setData()
方法:就复制clipboardData
的内容到到剪切板; - 如果取消了默认行为,而且没有调用
setData()
方法,就没有任何行为。
属性
Property | Type | Description |
---|---|---|
target 只读 |
EventTarget |
The event target (the topmost target in the DOM tree). |
type 只读 |
DOMString |
The type of event. |
bubbles 只读 |
Boolean |
Whether the event normally bubbles or not. |
cancelable 只读 |
Boolean |
Whether the event is cancellable or not. |
规范
Specification |
---|
Clipboard API and events # clipboard-event-copy |
HTML Standard # handler-oncopy |
浏览器兼容性
BCD tables only load in the browser
参见
HTMLElement.oncopy
- Related events