gpt4 book ai didi

javascript - 使用 JS 将任何标签内的文本复制到剪贴板

转载 作者:行者123 更新时间:2023-12-01 01:48:41 25 4
gpt4 key购买 nike

我需要复制 ~p~ 标签内的文本,我尝试使用此代码:

HTML:

<p id="copy">Text to copy</p>
<button onclick="copyFunction()">Copy text</button>

JS:

function copyFunction() {
var textToCopy = document.getElementById("copy");
textToCopy.select();
document.execCommand("copy");
alert("Copied the text: " + textToCopy.value);
}

但是没有成功。

最佳答案

function copyFunction() 
{
var $temp = $("<input>");
$("body").append($temp);
$temp.val($('#copy').text()).select();
document.execCommand("copy");
$temp.remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="copy">Text to copy</p>
<button onclick="copyFunction()">Copy text</button>

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

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