gpt4 book ai didi

javascript - 复制到剪贴板按钮

转载 作者:行者123 更新时间:2023-12-03 06:30:51 31 4
gpt4 key购买 nike

我想创建“复制到剪贴板”按钮来处理我们的共享点。它们应该放置在几个不同的地方,我需要复制的是页面上特定字段的一些文本(例如电子邮件列表)。

我知道,我可以选择文本并复制它,但我们经常这样做,因此拥有一个自动将文本复制到剪贴板的按钮会非常有用。

我确实设法在脚本编辑器中创建了一个脚本编辑器,我在其中粘贴了下面的整个代码(我在互联网上找到的)

<!DOCTYPE html>
<html>
<head>

<script type='text/javascript'>//<![CDATA[
window.onload=function(){
document.getElementById("copyButton").addEventListener("click", function() {
copyToClipboardMsg(document.getElementById("copyTarget"), "msg");
});

document.getElementById("copyButton2").addEventListener("click", function() {
copyToClipboardMsg(document.getElementById("copyTarget2"), "msg");
});

document.getElementById("pasteTarget").addEventListener("mousedown", function() {
this.value = "";
});


function copyToClipboardMsg(elem, msgElem) {
var succeed = copyToClipboard(elem);
var msg;
if (!succeed) {
msg = "Copy not supported or blocked. Press Ctrl+c to copy."
} else {
msg = "Text copied to the clipboard."
}
if (typeof msgElem === "string") {
msgElem = document.getElementById(msgElem);
}
msgElem.innerHTML = msg;
setTimeout(function() {
msgElem.innerHTML = "";
}, 2000);
}

function copyToClipboard(elem) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
if (isInput) {
// can just use the original source element for the selection and copy
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
} else {
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.textContent;
}
// select the content
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);

// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}

if (isInput) {
// restore prior selection
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
// clear temporary content
target.textContent = "";
}
return succeed;
}


}//]]>

</script>


</head>

<body>
<input id="copyTarget" value="Some initial text"> <button id="copyButton">Copy</button><br><br>
<span id="copyTarget2">Some Other Text</span> <button id="copyButton2">Copy</button><br><br>
<input id="pasteTarget"> Click in this Field and hit Ctrl+V to see what is on clipboard<br><br>
<span id="msg"></span><br>



</body>

</html>

但我们有主要问题:1)每次我们单击“复制”按钮时,它都会重新加载页面2)当我们考虑更新我们的文档时,它不是很优雅和友好

如果您对我提供任何有关如何使其更好地工作的提示,我将非常感激。

最佳答案

这个项目可能有帮助:clipboardjs

关于javascript - 复制到剪贴板按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38458798/

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