单击按钮“使用jQuery复制到剪贴板”
如何将div中的文本复制到剪贴板?我有一个div,需要添加一个链接,将文本添加到剪贴板。有解决办法吗单击按钮“使用jQuery复制到剪贴板”,jquery,html,css,Jquery,Html,Css,如何将div中的文本复制到剪贴板?我有一个div,需要添加一个链接,将文本添加到剪贴板。有解决办法吗 <p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p> <a class
<p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
<a class="copy-text">copy Text</a>
Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本
复制文本
单击“复制文本”后,按Ctrl+V键,必须粘贴该文本。从2016年开始编辑 从2016年起,您现在可以在大多数浏览器中将文本复制到剪贴板,因为大多数浏览器都能够使用对选定内容有效的
document.execCommand(“copy”)
以编程方式将选定的文本复制到剪贴板
与浏览器中的某些其他操作(如打开新窗口)一样,复制到剪贴板只能通过特定的用户操作(如鼠标单击)完成。例如,它不能通过计时器来完成
下面是一个代码示例:
document.getElementById(“copyButton”).addEventListener(“单击”,函数)(){
copyToClipboard(document.getElementById(“copyTarget”);
});
功能copyToClipboard(elem){
//创建隐藏的文本元素(如果它还不存在)
var targetId=“\u hiddencypytext”;
变量isInput=elem.tagName==“输入”| | elem.tagName==“文本区域”;
变量origSelectionStart、origSelectionEnd;
如果(输入){
//只能使用原始源元素进行选择和复制
目标=元素;
origSelectionStart=elem.selectionStart;
origSelectionEnd=elem.selectionEnd;
}否则{
//必须使用临时表单元素进行选择和复制
target=document.getElementById(targetId);
如果(!目标){
var target=document.createElement(“textarea”);
target.style.position=“绝对”;
target.style.left=“-9999px”;
target.style.top=“0”;
target.id=targetId;
document.body.appendChild(目标);
}
target.textContent=elem.textContent;
}
//选择内容
var currentFocus=document.activeElement;
target.focus();
target.setSelectionRange(0,target.value.length);
//复制所选内容
var成功;
试一试{
成功=document.execCommand(“复制”);
}捕获(e){
成功=错误;
}
//恢复原始焦点
if(currentFocus&&typeof currentFocus.focus==“函数”){
currentFocus.focus();
}
如果(输入){
//恢复先前选择
元素设置选择范围(origSelectionStart,origSelectionEnd);
}否则{
//清除临时内容
target.textContent=“”;
}
回归成功;
}
输入{
宽度:400px;
}
复制
要复制的文本在文本输入中,如:
,在按钮上单击上面的文本应复制到剪贴板,因此按钮类似:复制
您的脚本应如下所示:
<script language="JavaScript">
$(document).ready(function() {
var clip = new ZeroClipboard($("#copy_button"), {
moviePath: "ZeroClipboard.swf"
});
});
</script>
$(文档).ready(函数(){
var clip=新的零剪贴板($(“#复制_按钮”){
moviePath:“ZeroClipboard.swf”
});
});
对于CDN文件
- :
- :
ZeroClipboard.swf
和ZeroClipboard.js
“文件应与使用此功能的文件位于同一文件夹中,或者您必须像我们在页面上包含
一样包含
更新2020:此解决方案使用execCommand
。虽然在编写此答案时该功能还可以使用,但在许多浏览器上仍然可以使用,但不鼓励使用,因为可能会放弃支持
还有另一种非Flash方式(中提到的除外)。您需要选择文本,然后将当前在页面上选择的任何文本复制到剪贴板
例如,此函数将把传递的元素的内容复制到剪贴板中(在来自的注释中使用建议进行更新):
p1:我是第1段
p2:我是第二段
复制P1
副本P2
是一个很好的实用程序,它允许在不使用Flash的情况下将文本或HTML数据复制到剪贴板。它非常易于使用;只需包含.js并使用如下内容:
<button id='markup-copy'>Copy Button</button>
<script>
document.getElementById('markup-copy').addEventListener('click', function() {
clipboard.copy({
'text/plain': 'Markup text. Paste me into a rich text editor.',
'text/html': '<i>here</i> is some <b>rich text</b>'
}).then(
function(){console.log('success'); },
function(err){console.log('failure', err);
});
});
</script>
复制按钮
document.getElementById('markup-copy')。addEventListener('click',function(){
剪贴板。复制({
“文本/纯文本”:“标记文本。将我粘贴到富文本编辑器中。”,
“text/html”:“这里是一些富文本”
}).那么(
函数(){console.log('success');},
函数(err){console.log('failure',err);
});
});
2016年1月15日编辑:今天,我在2015年8月发布的答案中引用了相同的API。上一篇文章是指导用户使用ZeroClipboard。我只是想明确一点,我没有从jfriend00的答案中删除这个,相反,这是复制内容的最简单方法。
<div id="content"> Lorepm ispum </div>
<button class="copy" title="content">Copy Sorce</button>
function SelectContent(element) {
var doc = document
, text = doc.getElementById(element)
, range, selection
;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
document.execCommand('Copy');
}
$(".copy").click(function(){
SelectContent( $(this).attr('title'));
});
Lorepm ispum
复制攻击
功能选择内容(元素){
var doc=文档
,text=doc.getElementById(元素)
,范围,选择
;
if(doc.body.createTextRange){
range=document.body.createTextRange();
范围。移动到元素文本(文本);
range.select();
}else if(window.getSelection){
selection=window.getSelection();
range=document.createRange();
范围。选择节点内容(文本);
selection.removeAllRanges();
选择。添加范围(范围);
}
document.execComm
<div id="content"> Lorepm ispum </div>
<button class="copy" title="content">Copy Sorce</button>
function SelectContent(element) {
var doc = document
, text = doc.getElementById(element)
, range, selection
;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
document.execCommand('Copy');
}
$(".copy").click(function(){
SelectContent( $(this).attr('title'));
});
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="css/index.css" rel="stylesheet" />
<script src="js/jquery-2.1.4.min.js"></script>
<script>
function copy()
{
try
{
$('#txt').select();
document.execCommand('copy');
}
catch(e)
{
alert(e);
}
}
</script>
</head>
<body>
<h4 align="center">Copy your code</h4>
<textarea id="txt" style="width:100%;height:300px;"></textarea>
<br /><br /><br />
<div align="center"><span class="btn-md" onclick="copy();">copy</span></div>
</body>
</html>
<input id="result" style="width:300px"/>some example text
<button onclick="copyToClipboard('result')">Copy P1</button>
<input type="text" style="width:400px" placeholder="Paste here for test" />
function copyToClipboard(elementId) {
// Create a "hidden" input
var aux = document.createElement("input");
aux.setAttribute("value", document.getElementById(elementId).value);
// Append it to the body
document.body.appendChild(aux);
// Highlight its content
aux.select();
// Copy the highlighted text
document.execCommand("copy");
// Remove it from the body
document.body.removeChild(aux);
}
var ClipboardHelper = {
copyElement: function ($element)
{
this.copyText($element.text())
},
copyText:function(text) // Linebreaks with \n
{
var $tempInput = $("<textarea>");
$("body").append($tempInput);
$tempInput.val(text).select();
document.execCommand("copy");
$tempInput.remove();
}
};
ClipboardHelper.copyText('Hello\nWorld');
ClipboardHelper.copyElement($('body h1').first());
<div class="form-group">
<label class="font-normal MyText">MyText to copy</label>
<button type="button" class="btn btn-default btn-xs btnCopy" data="MyText">Copy</button>
</div>
$(".btnCopy").click(function () {
var element = $(this).attr("data");
copyToClipboard($('.' + element));
});
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
$("<textarea/>").appendTo("body").val(text).select().each(function () {
document.execCommand('copy');
}).remove();
$('button.copyButton').click(function(){
$(this).siblings('input.linkToCopy').select();
document.execCommand("copy");
});
<button class="copyButton">click here to copy</button>
<input class="linkToCopy" value="TEXT TO COPY"
style="position: absolute; z-index: -999; opacity: 0;" />
function CopyToClipboard(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select().createTextRange();
document.execCommand("copy");
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
document.execCommand("copy");
alert("text copied")
}}
<button id="button1" onclick="CopyToClipboard('div1')">Click to copy</button>
<div id="div1" >Text To Copy </div>
<textarea placeholder="Press ctrl+v to Paste the copied text" rows="5" cols="20"></textarea>
npm install clipboard-polyfill
window.clipboard = require('clipboard-polyfill');
var copyToClipboard = function (text) {
var $txt = $('<textarea />');
$txt.val(text)
.css({ width: "1px", height: "1px" })
.appendTo('body');
$txt.select();
if (document.execCommand('copy')) {
$txt.remove();
}
};
<input type="text" value="xxx" id="link" class="span12" />
<button type="button" class="btn btn-info btn-sm" onclick="copyToClipboard('#link')">
Copy Input Value
</button>
function copyToClipboard(element) {
$(element).select();
document.execCommand("copy");
}
<p style="color:wheat;font-size:55px;text-align:center;">How to copy a TEXT to Clipboard on a Button-Click</p>
<center>
<p id="p1">Hello, I'm TEXT 1</p>
<p id="p2">Hi, I'm the 2nd TEXT</p><br/>
<button onclick="copyToClipboard('#p1')">Copy TEXT 1</button>
<button onclick="copyToClipboard('#p2')">Copy TEXT 2</button>
</center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
<script>
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
</script>