2017-11-25 50 views
-1

這裏是我的HTML代碼:試圖讓一個搜索按鈕,可以重定向到另一個網址,並複製剪貼板上的重定向URL

<input id="text" type="text" name="text"> 
    <input type="button" name="page1" onclick="javascript:window.open('http://example.com/' + this.name + '/' + document.getElementById('text').value)"> 

如果你把在該領域的任何文本並單擊該按鈕它會重定向到http://example.com/page1/anytext 我試圖讓這個按鈕能夠重定向到URL並將其複製到剪貼板。因此,如果有任何訪問者在該字段上輸入anytext,它將複製網址http://example.com/page1/anytext並重定向到該網址。

+2

的可能的複製[?如何複製JavaScript中的剪貼板(https://stackoverflow.com/questions/400212/how-do-i-copy-to -the-clipboard-in-javascript) – Ali

回答

0

如下:

function myCopyFunction() { 
    var copyText = document.getElementById("page1"); 
    copyText.select(); 
    document.execCommand("Copy"); 
    alert("Copied the text: " + copyText.value); 
} 
+0

我必須這樣寫這段代碼嗎? ()「; alert(」copy「); alert(」copy「); (「複製文本:」+ copyText.value);}>' – user8481790

+0

將函數聲明放到HTML之外 –