我有overridden the paste
event .我注意到由于事件的默认行为被阻止,目前无法使用 Ctrl+Z 撤消“粘贴”。
$(this).on('paste', function (evt) {
// Get the pasted data via the Clipboard API.
// evt.originalEvent must be used because this is jQuery, not pure JS.
// https://stackoverflow.com/a/29831598
var clipboardData = evt.originalEvent.clipboardData || window.clipboardData;
var pastedData = clipboardData.getData('text/plain');
// Trim the data and set the value.
$(this).val($.trim(pastedData));
// Prevent the data from actually being pasted.
evt.preventDefault();
});
有没有办法覆盖撤消功能或以不同方式执行上述操作以使 Ctrl+Z 起作用?
相关问题
最佳答案
使用
document.execCommand("insertText", false, $.trim(pastedData));
代替
$(this).val($.trim(pastedData));
它将保留撤消历史记录。
$('#inputElement').on('paste', function (evt) {
var clipboardData = evt.originalEvent.clipboardData || window.clipboardData;
var pastedData = clipboardData.getData('text/plain');
this.select(); // To replace the entire text
document.execCommand("insertText", false, $.trim(pastedData));
evt.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="inputElement"></textarea>
关于javascript - 在 JS 中撤消覆盖的粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851542/