php点击复制按钮到我的粘贴板,jquery – 单击 – 复制到剪贴板

假设您的所有样式都是内联的,您需要获取元素的html而不是文本.就像是:

function copyToClipboard(element) {

var $temp = $("");

$("body").append($temp);

$temp.val($(element).html()).select(); //Note the use of html() rather than text()

document.execCommand("copy");

$temp.remove();

}

根据评论进行编辑:

要将格式复制到Gmail邮件正文或Word文档之类的内容,您必须实际选择该元素作为范围.当您将html内容插入textarea时,实际上是在复制原始文本.你想做这样的事情:

function copyToClipboard(element) { //Note, element should be a node rather than a jQuery instance.

var selection = window.getSelection(), //Get the window selection

selectData = document.createRange(); //Create a range

selection.removeAllRanges(); //Clear any currently selected text.

selectData.selectNodeContents(element); //Add the desired element to the range you want to select.

selection.addRange(selectData); //Highlight the element (this is the same as dragging your cursor over an element)

var copyResult = document.execCommand("copy"); //Execute the copy.

if(copyResult) //was the copy successful?

selection.removeAllRanges(); //Clear the highlight.

else

alert("Your browser does not support clipboard commands, press ctrl+c");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值