2017-08-07 157 views
-1

在我的代碼,我試圖使用複製到剪貼板按鈕複製iframe的代碼,但我並不滿足,因爲當我複製使用按鈕,它拷貝&lt;&gt;代替<>其次它贏得「T突出了文本區域,從而沒有任何替代的解決方案,以作爲複製的HTML代碼感謝 這是我的JSfiddlejQuery文檔複製到剪貼板

下面是一個示例複製的文本

&lt;iframe src='http://localhost/secvideo/cms/watch?v=30Rt9r' frameborder='0' style='overflow: hidden; position: absolute;' height='100%' width='100%'&gt;&lt;/iframe&gt;  

和這是我的JS

function copyToClipboard(elementId) { 
var aux = document.createElement("input"); 

// Assign it the value of the specified element 
aux.setAttribute("value", document.getElementById(elementId).innerHTML); 
document.body.appendChild(aux); 
aux.select(); 
document.execCommand("copy"); 
document.body.removeChild(aux); 
alert("Copied!"); 
} 
+0

你的JS小提琴是不是做如你所描述的 - 犯規復制的。 – jdmdevdotnet

+0

當您更改功能copyToClipboard(elementId)到window.copyToClipboard =功能(elementId)如期工作 – ajc2000

+0

https://jsfiddle.net/yhpe990k/ < - 因爲在這個小提琴做 – ajc2000

回答

0

我想你在這裏不必要地創建一個元素。您已經有一個帶有iframe內容的文本區域。

您只需要選擇文本區域並執行document.execCommand(「copy」);

修改你的腳本

window.copyToClipboard = function(elementId) { 

    // Create a "hidden" input 
    var aux = document.getElementById(elementId); 
    // Highlight its content 
    aux.select(); 
    // Copy the highlighted text 
    document.execCommand("copy"); 
    alert("Copied!"); 
} 

的jsfiddle https://jsfiddle.net/yhpe990k/1/

+0

像一個魅力工作 – Rtra