【发布时间】:2014-03-20 10:55:01
【问题描述】:
是否可以使用click EVENT执行复制命令?
我选择了一些文本,我希望在onClick 事件中复制这些文本,这样我就可以在不使用右键单击或 CTRL+C 复制文本。
【问题讨论】:
标签: javascript jquery onclick copy
是否可以使用click EVENT执行复制命令?
我选择了一些文本,我希望在onClick 事件中复制这些文本,这样我就可以在不使用右键单击或 CTRL+C 复制文本。
【问题讨论】:
标签: javascript jquery onclick 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除外)。
【讨论】:
HTML
<form name="myForm">
<span onclick="copyText(this)" >Text1</span>, <span onclick="copyText(this)" >Text2</span>
<br>
<input name="myField"></input>
JavaScript
function copyText(element) {
document.myForm.myField.value = element.innerHTML;
}
复制到剪贴板 Ctrl+C
$("#text1").click(function(){
var holdtext = $("#clipboard").innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
});
【讨论】:
使用getselection() 在浏览器窗口中获取选定的文本
【讨论】: