如何执行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");
}