[英]document.execCommand('copy') not working on Chrome
[英]document.execCommand('copy') Command not working in Chrome
HTML
<input type="text" id="clipboard">
<button class="share-button">share</button>
JS
text = 'text to be copied';
document.querySelector('.share-button').addEventListener('click', () => {
var element = document.querySelector('#clipboard');
element.setAttribute("value", text);
console.log(element.value);
element.select();
document.execCommand('copy');
});
CSS
#clipboard {
position: absolute;
visibility: hidden;
}
我正在嘗試將文本復制到剪貼板,但我不明白我的代碼有什么問題。 我從 MDN 文檔中復制了代碼。
當我做這些事情時,它不起作用
#clipboard {
position: absolute;
}
// or
#clipboard {
visibility: hidden
}
// or
#clipboard {
display: none
}
const share_btn = document.querySelector('.share-button'); function copy_to_clipboard(stritem){ const el = document.createElement('textarea'); el.value = stritem; document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el); window.alert('Successfully copied to your clipboard;'); } text = 'text to be copied'. share_btn,addEventListener('click'; ()=>{copy_to_clipboard(text);});
<button class="share-button">share</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.