gpt4 book ai didi

javascript - 不使用任何输入将文本复制到剪贴板

转载 作者:行者123 更新时间:2023-12-05 08:14:48 24 4
gpt4 key购买 nike

刚刚在网上看到很多文章,找到了将文本复制到剪贴板的解决方案。但是每个教程都会用输入示例进行解释。

  function GeeksForGeeks() {
var copyGfGText = document.getElementById("GfGInput");
copyGfGText.select();

document.execCommand("copy");

alert("Copied the text: " + copyGfGText.value);
}
  <input type="text" value="GeeksForGeeks" id="GfGInput">

<!-- The button used to copy the text -->
<button onclick="GeeksForGeeks()">Copy text</button>

但我只需要复制一个简单的文本。有没有办法将简单的字符串从变量复制到剪贴板?例子`

let text = "copy this text to the clipboard";

最佳答案

您应该可以像这样使用 document.createElement(); 来做到这一点;

function CopyMe(TextToCopy) {
var TempText = document.createElement("input");
TempText.value = TextToCopy;
document.body.appendChild(TempText);
TempText.select();

document.execCommand("copy");
document.body.removeChild(TempText);

alert("Copied the text: " + TempText.value);
}
<button onclick="CopyMe('The text here will be copied')">Copy text</button>

让我知道这有什么帮助。

关于javascript - 不使用任何输入将文本复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63033012/

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