当尝试在安卓的Chrome浏览器上使用navigator.clipboard.writeText()复制一些文本时,只要我之后不显示警告框,它就能正常工作。一旦我显示了一个alert(),它就不再工作了。
例如,下面的代码按预期工作正常:
function myFunction() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999);
navigator.clipboard.writeText(copyText.value);
}
<input type="text" value="Hello world" id="myInput" style="width:auto"> <button onclick="myFunction()">copy</button>
然而下面的代码不起作用,在控制台中不会抛出任何错误,并且在PC上的Chrome浏览器上正常工作,但在安卓上不行。
function myFunction()
{
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999);
navigator.clipboard.writeText(copyText.value);
alert("成功复制了文本")
}
<input type="text" value="Hello world" id="myInput" style="width:auto" > <button onclick="myFunction()" >copy</button>
有人知道发生了什么吗?谢谢。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号