javascript - HTML 复制到剪贴板按钮不起作用
问题描述
我正在尝试做一个按钮来将链接(www.google.com)复制到剪贴板,但它不起作用。我的代码是:
<script type='text/javascript'>
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("www.google.com");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
navigator.clipboard.writeText(copyText.value);
/* Alert the copied text */
alert("Copied link!");
}
</script>
和按钮:
<button onclick='myFunction()'>Clik to copy the Link</button>
解决方案
尝试使用异步功能并等待
async function copyGoogleLink(){
await navigator.clipboard.writeText('https://www.google.com')
.then(() => console.log('Copied'));
}
将此与事件侦听器一起使用
推荐阅读
- powershell - 将 CSV 文件列导出到其他 CSV 文件
- java - 一个带开关盒的简单计算器
- c++ - 将函数作为参数传递给类 C++
- ionic-framework - 防止硬件后退按钮使用android关闭模式
- ios - 如何更新应用商店链接。我可以像示例一样更改应用商店网址吗
- android - Navigation Drawer 用另一个有问题地保留抽屉选项的片段替换片段
- java - FitNesse with JUnit:并行执行套件
- powerbi - 忽略堆积条形图中的空白
- javascript - 无法解决 javascript 中的承诺
- java - 哪个存储库具有 eXist 以及如何使用 gradle 将其添加到类路径中?