我在从代码创建 ClipboardEvent 时遇到了问题。未定义创建事件中的 clipboardData 对象。
可能有人对此有所了解?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input id="testPasteInput">
<button onclick="pasteFromCode()">Paste</button>
<script>
const testPasteInput = document.getElementById('testPasteInput');
testPasteInput.addEventListener('paste', ({clipboardData}) => console.log(clipboardData.getData('text')));
function pasteFromCode() {
const clipboardEvent = new ClipboardEvent('paste', { dataType: 'text/plain', data: '123' });
testPasteInput.dispatchEvent(clipboardEvent);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)