简体   繁体   English

我在 JavaScript 中复制到剪贴板代码不起作用

[英]my copy to clipboard code in JavaScript doesn't work

as you would have guessed from my question and as you will interpret by looking at my code , im a beginner.正如您从我的问题中猜到的那样,并且您将通过查看我的代码来解释,我是初学者。 this is the first real application i tried to write in javascript and i would love to understand why it doesn't work?这是我尝试用 javascript 编写的第一个真正的应用程序,我很想知道为什么它不起作用?

var button = document.getElementById("button");
var text = document.getElementById("text");
function button() {
 text.select();
 text.setSelectionRange(0, 99999); 
navigator.clipboard.writeText(text.value);
alert("you copied this text");
}

button is a onclick event in html and text is an input in html.按钮是 html 中的 onclick 事件,文本是 html 中的输入。 thank you very much非常感谢您

As mentioned, the writeText is a promise, but also the Clipboard API needs permision.如前所述, writeText是一个承诺,但Clipboard API需要许可。 Check this if the permission is granted or not检查此是否授予权限

 function myFunction() { /* Get the text field */ var copyText = document.getElementById("myText"); /* 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).then(function() { alert("Copied to clipboard successfully!"); }, function(error) { alert("ERROR:\\n"+error); });; } async function CheckPermission(){ const readPerm = await navigator.permissions.query({name: 'clipboard-read', allowWithoutGesture: false }); const writePerm = await navigator.permissions.query({name: 'clipboard-write', allowWithoutGesture: false }); // Will be 'granted', 'denied' or 'prompt': alert('Read: '+readPerm.state+'\\nWrite: '+writePerm.state); }
 <input type="text" value="ASDF" id="myText"> <button onclick="myFunction()">Copy text</button><br><br> <button onclick="CheckPermission()">Check Permission</button>

If so, need to ask permission.如果是这样,需要征得许可。 More info here .更多信息在这里

i don't know if i should write this as an answer or what.我不知道我是否应该写这个作为答案或什么。 but anyways i rewrote the code and it ended up like this :但无论如何我重写了代码,结果是这样的:

var text = document.getElementById("text");
var button = document.getElementById("button");

function textinput(selector) {
text.select();
text.setSelectionRange(0, 99999); 
}
function buttonholder() {
navigator.clipboard.writeText(text.value);
}
button.addEventListener("click",buttonholder);

thank you everyone that replied.谢谢大家回复。 the copy part is working and its ok.复制部分正在工作并且没问题。 but in the code above as you can see i made a function that would select the input.但是在上面的代码中,如您所见,我创建了一个可以选择输入的函数。 but i wonder how can i call it?但我想知道我该怎么称呼它? i tried some typical code but couldn't puzzle it in. again thanks for helping我尝试了一些典型的代码,但无法解决。再次感谢您的帮助

You've already used "function" as an identifier for your button element.您已经使用“function”作为按钮元素的标识符。 So just change your function's name (eg buttonHandler) and it would work.因此,只需更改您的函数名称(例如 buttonHandler),它就会起作用。

Answer Code :答案代码:

var button = document.getElementById("button");
var text = document.getElementById("text");
function buttonHandler() { 
navigator.clipboard.writeText(text.value);
alert(`you copied this ${text.value}`);
}

PS.附注。 don't forget to change the name of the function in your HTML file.不要忘记更改 HTML 文件中函数的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM