我正在創建一個小站點來使用 asin 生成亞馬遜會員鏈接我使用了一個小腳本來生成 URL,但我希望直接在剪貼板中復制輸出。
我環顧四周,沒有找到適合我的問題的解決方案。
這是我用來生成 URL 的腳本。
HTML 部分只是一個輸入和一個按鈕。
<script>
function myFunction() {
let userInput = document.querySelector("#userInput");
let url = document.querySelector("#url");
url.innerHTML = "https://www.amazon.it/dp/" userInput.value "/ref=nosim?tag=altpe-21";
}
</script>
uj5u.com熱心網友回復:
如果您想要一個簡單的解決方案,您必須在鏈接已經創建并在網站中創建后創建另一個函式。
新函式將使用document.getElementById()函式和.select()方法
示例和實作可以在這里找到:
https://www.w3schools.com/howto/howto_js_copy_clipboard.asp
uj5u.com熱心網友回復:
您可以為此目的使用ClipboardAPI 。navigator
<script>
function myFunction() {
let userInput = document.querySelector("#userInput");
let url = document.querySelector("#url");
let output = "https://www.amazon.it/dp/" userInput.value "/ref=nosim?tag=altpe-21";
url.innerHTML = output;
navigator.permissions.query({name: "clipboard-write"}).then(result => {
if (result.state == "granted" || result.state == "prompt") {
navigator.clipboard.writeText(output);
}
});
}
</script>
uj5u.com熱心網友回復:
這是簡單的解決方案:
/* Get the text field */
var copyText = document.getElementById("myInput");
/* 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);
/* Alert the copied text */
alert("Copied the text: " copyText.value);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/496767.html
標籤:javascript html
上一篇:洗掉特定螢屏尺寸的底部邊框
