gpt4 book ai didi

javascript - 无法在 Javascript 中将文本复制到剪贴板

转载 作者:行者123 更新时间:2023-11-28 12:11:33 25 4
gpt4 key购买 nike

我在尝试了解如何将文本复制到剪贴板的不同方法时遇到了很多问题。如果有人可以向我解释为什么我的代码不起作用,我将非常感激。

<!DOCTYPE HTML>
<html dark= "true" style="font-size: 50px;font-family: Roboto, Arial, sans-serif">
<body>
<textarea id="copytext">ROGER</textarea>
<div class="button" id="adadad">
<button onclick="copyS()" id="dlld">
</button>
</div>
<p> </p>
<script type=text/javascript">
function copyS{
clicked = document.getElementById("dlld");
}
if ("dlld" == clicked){
var am1 = document.getElementById("copytext");
document.execCommand("copy");
}
}
</script>
<p> </p>
</body>
</html>

最佳答案

好的,我对您的代码做了一些调整,但它现在应该可以工作了。

<!DOCTYPE HTML>
<html dark= "true" style="font-size: 50px;font-family: Roboto, Arial, sans-serif">
<body>
<textarea id="copytext">ROGER</textarea>
<div class="button" id="adadad">
<button onclick="copyS()" id="dlld">Copy text</button>
</div>
<p> </p>

<!-- removed the single " at the end of your script tag -->
<script type=text/javascript>
//added () after you declared your function
function copyS(){
clicked = document.getElementById("dlld");
//added .id to clicked. Not sure why you need to do this, but it works now
if ("dlld" == clicked.id){
var am1 = document.getElementById("copytext");
//you need to select before running the .execCommand
am1.select();
document.execCommand("copy");
}
}
</script>

<p> </p>
</body>
</html>

关于javascript - 无法在 Javascript 中将文本复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61020388/

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