【发布时间】:2018-01-11 14:59:24
【问题描述】:
我遇到了从代码创建 ClipboardEvent 的问题。未定义已创建事件中的剪贴板数据对象。
有人知道吗?
<!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>
【问题讨论】:
-
为什么不直接设置值?
-
@baao 这只是一个例子。我有一种情况需要调度 ClipboardEvent
标签: javascript html