Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何执行ctrl+;c或在单击按钮时使用javascript或jquery复制commad_Javascript_Jquery_Onclick_Copy - Fatal编程技术网

如何执行ctrl+;c或在单击按钮时使用javascript或jquery复制commad

如何执行ctrl+;c或在单击按钮时使用javascript或jquery复制commad,javascript,jquery,onclick,copy,Javascript,Jquery,Onclick,Copy,是否可以使用单击事件执行复制命令 我选择了一些文本,我希望在onClick事件中复制此文本,这样我就可以通过右键单击或CTRL+C将此文本复制到另一个页面上。

是否可以使用单击事件执行复制命令

我选择了一些文本,我希望在
onClick
事件中复制此文本,这样我就可以通过右键单击或CTRL+C将此文本复制到另一个页面上。

<form name="myForm">
<span onclick="copyText(this)" >Text1</span>, <span onclick="copyText(this)" >Text2</span>
<br>
<input name="myField"></input>
复制到剪贴板Ctrl+C

$("#text1").click(function(){
var holdtext = $("#clipboard").innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
});
用于在浏览器窗口中获取选定文本

function copyText(){
    var txt = '';
     if (window.getSelection)
        txt = window.getSelection();
    else if (document.getSelection)
        txt = document.getSelection();
    else return;
    document.getElementById("a").value=txt;
    allCopied =document.getElementById("a").createTextRange();
    allCopied.execCommand("RemoveFormat");

   allCopied.execCommand("Copy");
}
但出于安全原因,大多数浏览器不允许修改剪贴板(除了Internet explorer)

我认为不是“document.getElementById(“a”).value=“txt”,而是“document.getElementById(“a”).value=txt”;但除此之外,这对我帮助很大。谢谢
function copyText(){
    var txt = '';
     if (window.getSelection)
        txt = window.getSelection();
    else if (document.getSelection)
        txt = document.getSelection();
    else return;
    document.getElementById("a").value=txt;
    allCopied =document.getElementById("a").createTextRange();
    allCopied.execCommand("RemoveFormat");

   allCopied.execCommand("Copy");
}