Javascript 单击文本时将文本复制到剪贴板
我有一个网站,邀请链接显示在Javascript 单击文本时将文本复制到剪贴板,javascript,html,Javascript,Html,我有一个网站,邀请链接显示在标签中 我只想在单击文本时将文本复制到剪贴板 我的代码如下所示: <script> function CopyInviteLink() { var copyText = document.getElementById("invite-link"); copyText.select(); copyText.setSelectionRange(0, 99999) document.execCommand("copy");
标签中
我只想在单击文本时将文本复制到剪贴板
我的代码如下所示:
<script>
function CopyInviteLink() {
var copyText = document.getElementById("invite-link");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
}
function setInviteLink(roomID) {
const inviteLink = window.location.href + "/join.html?room=" + roomID;
document.getElementById("invite-link").innerText = inviteLink;
}
</script>
<h2 id="invite-link" onclick="myFunction()"></h2><br>
函数CopyInviteLink(){
var copyText=document.getElementById(“邀请链接”);
copyText.select();
copyText.setSelectionRange(0,99999)
文件。执行命令(“副本”);
}
功能设置邀请链接(roomID){
const inviteLink=window.location.href+“/join.html?room=“+roomID;
document.getElementById(“邀请链接”).innerText=inviteLink;
}
我试过这个方法:
它使用以下方法:(document.execCommand(“copy”)
)但这似乎只适用于
,但我只希望有一个干净的文本,而不是“文本输入”样式
有人能帮我或链接一个解决方案吗?这对您很有用
这里的textToCopy
是input
var input = document.createElement("textarea");
document.body.appendChild(input);
input.value = document.getElementById("invite-link").value;
input.select();
document.execCommand("copy");
document.body.removeChild(input);
const copyToClipboard=str=>{
const el=document.createElement('textarea');
el.value=document.getElementById(“邀请链接”).innerHTML;
el.setAttribute('只读','');
el.style.position='绝对';
el.style.left='-9999px';
文件.正文.附件(el);
el.select();
document.execCommand('copy');
警报(“复制文本:+el.value”);
文件.正文.删除文件(el);
};代码>
标题
是否回答了您的问题?