HTMLElement:paste 事件

Invalid slug for templ/sidebar: conflicting/Web/API/Element/paste_event_4ff5a40c4d4f891019aed89389e3526001e15ebf9526309428c754b4b9db12f6

paste 事件在用户通过浏览器的用户界面开始粘帖操作时触发。

语法

在类似 addEventListener() 这样的方法中使用事件名称,或设置事件处理器属性。

js
addEventListener("paste", (event) => {});

onpaste = (event) => {};

事件类型

事件属性

也从其父接口 Event 继承属性。

ClipboardEvent.clipboardData 只读

一个 DataTransfer 对象,其包含用户发起的 cutcopypaste 操作所影响的数据,以及它的 MIME 类型。

示例

此示例记录每一次对 <textarea> 的复制和粘贴的尝试。

HTML

html
<h3>试一下这个文本区域:</h3>
<textarea id="editor" rows="3">
尝试将文本复制并粘贴到该字段中!
</textarea>

<h3>日志:</h3>
<p id="log"></p>

JavaScript

js
function logCopy(event) {
  log.innerText = `已复制!\n${log.innerText}`;
}

function logPaste(event) {
  log.innerText = `已粘贴!\n${log.innerText}`;
}

const editor = document.getElementById("editor");
const log = document.getElementById("log");

editor.oncopy = logCopy;
editor.onpaste = logPaste;

结果

规范

No specification found

No specification data found for undefined.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

浏览器兼容性

参见