有个需求要修改粘贴里边的内容。但是无法做到快捷键撤回,但是原生的是可以撤回的。代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="wrap">
<div
id="editableDiv"
contenteditable="true"
suppressContentEditableWarning="true"
style="width: 500px; min-height: 50px; border: 1px solid #ccc"
>
text1
</div>
<script>
editableDiv.onpaste = (e) => {
let paste = e.clipboardData.getData("text");
const selection = window.getSelection() || {};
if (!selection.rangeCount) return false;
selection.deleteFromDocument();
//对要粘贴的内容做处理 代码省略
let newPaste = paste;
let range = selection.getRangeAt(0);
let newDom = document.createTextNode(newPaste);
range.insertNode(newDom);
range.setStartAfter(newDom);
e.preventDefault();
};
</script>
</div>
</body>
</html>
1、在复制时修改剪切板的内容。
2、拦截粘贴事件。