gpt4 book ai didi

javascript在textarea中捕获粘贴事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:30 26 4
gpt4 key购买 nike

我目前有一个文本区域,我需要控制粘贴的文本,

本质上,我需要能够将用户想要粘贴到文本区域中的任何内容放入变量中。

然后我会计算出他们粘贴文本的位置和字符串的大小,以便将其从文本区域中删除,

然后最后以我自己的方式处理变量中的文本。

我的问题:如何获取用户刚刚粘贴的变量中文本的副本?

谢谢。

最佳答案

我前几天回答过类似的问题:Detect pasted text with ctrl+v or right click -> paste .这次我包含了一个相当长的函数,可以在 IE 中准确获取文本区域中的选择边界;剩下的就比较简单了。

您可以使用粘贴事件来检测大多数浏览器中的粘贴(但尤其不是 Firefox 2)。当您处理粘贴事件时,记录当前选择,然后设置一个简短的计时器,在粘贴完成后调用一个函数。然后此函数可以比较长度以了解在何处查找粘贴的内容。类似于以下内容:

function getSelectionBoundary(el, start) {
var property = start ? "selectionStart" : "selectionEnd";
var originalValue, textInputRange, precedingRange, pos, bookmark, isAtEnd;

if (typeof el[property] == "number") {
return el[property];
} else if (document.selection && document.selection.createRange) {
el.focus();

var range = document.selection.createRange();
if (range) {
// Collapse the selected range if the selection is not a caret
if (document.selection.type == "Text") {
range.collapse(!!start);
}

originalValue = el.value;
textInputRange = el.createTextRange();
precedingRange = el.createTextRange();
pos = 0;

bookmark = range.getBookmark();
textInputRange.moveToBookmark(bookmark);

if (/[\r\n]/.test(originalValue)) {
// Trickier case where input value contains line breaks

// Test whether the selection range is at the end of the
// text input by moving it on by one character and
// checking if it's still within the text input.
try {
range.move("character", 1);
isAtEnd = (range.parentElement() != el);
} catch (ex) {
log.warn("Error moving range", ex);
isAtEnd = true;
}
range.moveToBookmark(bookmark);

if (isAtEnd) {
pos = originalValue.length;
} else {
// Insert a character in the text input range and use
// that as a marker
textInputRange.text = " ";
precedingRange.setEndPoint("EndToStart", textInputRange);
pos = precedingRange.text.length - 1;

// Delete the inserted character
textInputRange.moveStart("character", -1);
textInputRange.text = "";
}
} else {
// Easier case where input value contains no line breaks
precedingRange.setEndPoint("EndToStart", textInputRange);
pos = precedingRange.text.length;
}
return pos;
}
}
return 0;
}

function getTextAreaSelection(textarea) {
var start = getSelectionBoundary(textarea, true),
end = getSelectionBoundary(textarea, false);

return {
start: start,
end: end,
length: end - start,
text: textarea.value.slice(start, end)
};
}

function detectPaste(textarea, callback) {
textarea.onpaste = function() {
var sel = getTextAreaSelection(textarea);
var initialLength = textarea.value.length;
window.setTimeout(function() {
var val = textarea.value;
var pastedTextLength = val.length - (initialLength - sel.length);
var end = sel.start + pastedTextLength;
callback({
start: sel.start,
end: end,
length: pastedTextLength,
text: val.slice(sel.start, end),
replacedText: sel.text
});
}, 1);
};
}

window.onload = function() {
var textarea = document.getElementById("your_textarea");
detectPaste(textarea, function(pasteInfo) {
var val = textarea.value;

// Delete the pasted text and restore any previously selected text
textarea.value = val.slice(0, pasteInfo.start) +
pasteInfo.replacedText + val.slice(pasteInfo.end);

alert(pasteInfo.text);
});
};

关于javascript在textarea中捕获粘贴事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3239124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com