2020年更新:此解决方案使用execCommand
。虽然在撰写本答案时该功能很好,但现在已被视为过时。它仍将在许多浏览器上工作,但不建议使用,因为支持可能会被放弃。
除了Clipboard API(在jfriend00的回答中提到)之外,还有另一种非Flash方式。您需要选择文本,然后执行命令copy
,以将当前在页面上选定的任何文本复制到剪贴板上。
例如,此函数将把传递元素的内容复制到剪贴板中(根据PointZeroTwo在评论中的建议进行更新):
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
这是它的工作原理:
- 创建一个临时隐藏的文本字段。
- 将元素的内容复制到该文本字段中。
- 选择文本字段的内容。
- 执行命令复制,如:
document.execCommand("copy")
。
- 删除临时文本字段。
注意,元素的内部文本可能包含
空格。因此,如果您想将其用于密码等用途,则可以使用上面代码中的
$(element).text().trim()
修剪文本。
您可以在此处查看快速演示:
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p id="p1">P1: I am paragraph 1</p>
<p id="p2">P2: I am a second paragraph</p>
<button onclick="copyToClipboard('#p1')">Copy P1</button>
<button onclick="copyToClipboard('#p2')">Copy P2</button>
<br/><br/><input type="text" placeholder="Paste here for test" />
主要问题在于目前并非所有的浏览器都支持这个功能,但您可以在以下主要浏览器上使用:
- Chrome 43
- Internet Explorer 10
- Firefox 41
- Safari 10
更新1:这也可以通过纯JavaScript解决方案实现(无需jQuery):
function copyToClipboard(elementId) {
var aux = document.createElement("input");
aux.setAttribute("value", document.getElementById(elementId).innerHTML);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
}
<p id="p1">P1: I am paragraph 1</p>
<p id="p2">P2: I am a second paragraph</p>
<button onclick="copyToClipboard('p1')">Copy P1</button>
<button onclick="copyToClipboard('p2')">Copy P2</button>
<br/><br/><input type="text" placeholder="Paste here for test" />
注意,现在我们传递id时不需要使用#符号。
如下评论区
madzohan所述,在某些情况下(本地运行文件时),64位版本的Google Chrome存在一些奇怪的问题。使用上述非jQuery解决方案可以解决此问题。
Madzohan尝试在Safari中使用
document.execCommand('SelectAll')
而不是使用
.select()
(如聊天和下面的评论所指定)来解决该问题,而该解决方案也奏效了。
正如
PointZeroTwo在评论中所指出,可以改进代码以返回成功/失败结果。您可以在
this jsFiddle上查看演示。
更新:复制保留文本格式
正如用户在西班牙版StackOverflow上指出的那样,如果您想要逐字复制元素的内容,则上面列出的解决方案完美地工作,但如果您想要带格式粘贴已复制的文本(因为它被复制到了一个input type="text"
中,所以格式会丢失),则效果不佳。
解决方案是将其复制到可编辑的div
中,然后使用类似的方式使用execCommand
进行复制。这里有一个示例-单击复制按钮,然后粘贴到下面的可编辑框中:
function copy(element_id){
var aux = document.createElement("div");
aux.setAttribute("contentEditable", true);
aux.innerHTML = document.getElementById(element_id).innerHTML;
aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)");
document.body.appendChild(aux);
aux.focus();
document.execCommand("copy");
document.body.removeChild(aux);
}
#target {
width:400px;
height:100px;
border:1px solid #ccc;
}
<p id="demo"><b>Bold text</b> and <u>underlined text</u>.</p>
<button onclick="copy('demo')">Copy Keeping Format</button>
<div id="target" contentEditable="true"></div>
而在jQuery中,它会像这样:
function copy(selector){
var $temp = $("<div>");
$("body").append($temp);
$temp.attr("contenteditable", true)
.html($(selector).html()).select()
.on("focus", function() { document.execCommand('selectAll',false,null); })
.focus();
document.execCommand("copy");
$temp.remove();
}
#target {
width:400px;
height:100px;
border:1px solid #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p id="demo"><b>Bold text</b> and <u>underlined text</u>.</p>
<button onclick="copy('#demo')">Copy Keeping Format</button>
<div id="target" contentEditable="true"></div>