[英]Pasting from clipboard into textarea using JavaScript onclick
I'm trying to implement the following scripts to allow me to paste from my clipboard into a textarea on my webpage in Chrome.我正在尝试实现以下脚本,以允许我从剪贴板粘贴到 Chrome 网页上的文本区域。 They both demo perfectly on jsfiddle.net but I can't get it to work on my website.
它们都在 jsfiddle.net 上完美演示,但我无法在我的网站上使用它。 On jsfiddle, it prompted a warning to allow pasting from clipboard, as expected.
在 jsfiddle 上,正如预期的那样,它提示允许从剪贴板粘贴的警告。 On my site it did not.
在我的网站上没有。 I also temporarily deleted any other files in the same folder invoking .js src scripts in case they were somehow interfering or overriding this code.
我还临时删除了同一文件夹中调用 .js src 脚本的任何其他文件,以防它们以某种方式干扰或覆盖此代码。 (@Rob Louie - thank you for initialing posting these links a few years ago. I couldn't comment on that thread directly due to my newbie status.) Any help would be appreciated.
(@Rob Louie - 感谢您几年前首次发布这些链接。由于我的新手身份,我无法直接对该线程发表评论。)任何帮助将不胜感激。 Thank you.
谢谢你。
https://jsfiddle.net/1vmansr2/ https://jsfiddle.net/1vmansr2/
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
navigator.clipboard.readText()
.then(text => {
document.getElementById("demo").innerHTML = text;
})
.catch(err => {
document.getElementById("demo").innerHTML = 'Failed to read clipboard contents: '+err;
});
}
</script>
https://jsfiddle.net/zm490d6a/ https://jsfiddle.net/zm490d6a/
<style>
textarea {
width: 300px;
height: 400px;
}
</style>
<script async src="//jsfiddle.net/zm490d6a/embed/"></script>
<textarea onclick="paste(this)"></textarea>
<script>
async function paste(input) {
const text = await navigator.clipboard.readText();
input.value = text;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.