[英]How can I send an automatically generated ID for a form element to a javascript function?
[英]How can i send text from element with id to function on click?
我希望當我按下按鈕時它會復制“你好”但我收到這個錯誤:copyText.select 不是一個函數
如何解決?
<html>
<head>
</head>
<body>
<span id="aaa">hello</span>
<button onclick="copy('#aaa')">copy</button>
<script>
function copy(text)
{
var copyText = text;
copyText.select();
document.execCommand("copy");
console.log(document.getElementById('text'));
}
</script>
</body>
您不能使用選擇功能選擇跨度的文本。 您必須使用跨度文本的值創建一個臨時輸入。 使用 select() 選擇輸入中的值並復制文本,然后刪除輸入
function copy(text) { var copyText = document.getElementById(text).textContent; document.querySelector('#aux').innerHTML+=('<input id="a" value='+copyText+'>') document.getElementById("a").select(); document.execCommand("copy"); document.querySelector('#aux').innerHTML=""; //console.log(document.getElementById('text')); }
<html> <head> </head> <body> <span id="aaa">hello</span> <button onclick="copy('aaa')">copy</button> <span id="aux"></span> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.