2017-08-07 154 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