我有这样的代码,有时起作用,有时不起作用。我不太关心这些话题,但我需要修复它。你能帮我解决我的问题吗?
这段代码
function CopyToClipboard(containerid){if(document.selection)
{var range=document.body.createTextRange();range.moveToElementText(document.getElementById(containerid));range.select().createTextRange();document.execCommand("copy");}
else if(window.getSelection){var range=document.createRange();range.selectNode(document.getElementById(containerid));window.getSelection().addRange(range);
document.execCommand("copy");$("#text-3").fadeIn().delay(2000).fadeOut()}}
function myFunction(){var copyText=document.getElementById("myInput");copyText.select();copyText.setSelectionRange(0,99999);document.execCommand("copy");$("#text-2").fadeIn().delay(2000).fadeOut();}html
<button name="copy text" onclick="CopyToClipboard('div1')"></button>
<div id="div1"><h3>my text</h3><br></div>
<div id="text-2">notif 2</div>
<div id="text-3">notif 3</div>我正在为本例寻找一种最新的代码,不同的浏览器对此代码没有问题,我将在这里发布代码。
发布于 2022-11-26 12:02:23
试试下面的样子,
function copyToClipboard() {
navigator.clipboard.writeText(document.getElementById('div1').innerText);
//alert('data copied');
document.getElementById('messageis').style.display = "block";
setTimeout(function() {
document.getElementById('messageis').style.display = "none";
}, 3000);
} #messageis
{
display: none;
background: red;
padding: 10px;
position: absolute;
bottom: 10px;
left:30%;
}<button name="copy text" onclick="copyToClipboard()">Copy</button>
<div><h3 class="rt rt-13 rt-medium" id="div1">my text</h3><br></div>
<div id="messageis">Code Copied</div>
https://stackoverflow.com/questions/74581688
复制相似问题