如何将textarea的内容复制到剪贴板?

时间:2012-11-27 13:51:45

标签: javascript jquery

  

可能重复:
  How to copy to the clipboard in JavaScript?

我有一个html markup,它们被回复并包含在textarea中,它也只是一个只读:

<textarea rows="4" cols="70" readonly="readonly">text with markup goes here.</textarea>

如何在用户点击只读文本区域内容时自动选择并将其复制到剪贴板并发出已复制的通知?

2 个答案:

答案 0 :(得分:2)

PHP是服务器端,因此您将无法执行任何与客户端交互的操作。在客户端方面,javascript可以做点什么。坏消息是,据我所知,Firefox不再允许这种情况(我记得在TinyMCE的剪贴板功能方面看到了一些关于它的争论),所以你可能需要混合flash和js以获得完全交叉的东西。浏览器解决方案。

我可能因此建议http://code.google.com/p/zeroclipboard/作为一种可能性吗?

答案 1 :(得分:0)

为什么你想要PHP,试试这个:

function copy(inElement) {
 if (inElement.createTextRange) {
  var range = inElement.createTextRange();
 if (range)
   range.execCommand('Copy');
} else {
  var flashcopier = 'flashcopier';
  if(!document.getElementById(flashcopier)) {
  var divholder = document.createElement('div');
   divholder.id = flashcopier;
  document.body.appendChild(divholder);
}
   document.getElementById(flashcopier).innerHTML = '';
   var divinfo = '<embed src="_clipboard.swf"        FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0"      type="application/x-shockwave-flash"></embed>';
document.getElementById(flashcopier).innerHTML = divinfo;
}
 }

<form name="formtocopy" action="" >
<textarea name="texttocopy" disabled="disabled">
A whole bunch of text here that will be copied.
</textarea>
<br>
<a href="javascript:copy(document.formtocopy.texttocopy);">Copy the Text!</a>
</form>