我創建了自己的投資組合網站,在導航欄上,有我的電子郵件。我希望任何點擊此電子郵件的人將其復制到剪貼板。這就是我嘗試過的,但它不起作用!
<button onclick="copy()" id="copy">Copy</button>
function copy() {
var copyText = document.querySelector("#copy");
copyText.select(); document.execCommand("copy");}
document.querySelector("#copy").addEventListener("click", copy);
uj5u.com熱心網友回復:
請注意,通過 Chrome 66 中的權限 API,可以“請求權限”并測驗對剪貼板的訪問權限。
var text = "text to be copied"
navigator.clipboard.writeText(text).then(_ => {
console.log('copied successfully!')
})
uj5u.com熱心網友回復:
按鈕沒有選擇方法,文本框有。
您可以使用文本輸入來保存要復制到剪貼板的電子郵件地址。
function copy() {
var copyText = document.querySelector("#email");
copyText.select(); document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
input {margin:-100%}
<button onclick="copy()" id="copy">Copy</button>
<input id=email value="[email protected]">
按鈕沒有選擇方法,文本框有。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406837.html
標籤:
