How do I copy the contents of a textarea to the clipboard?
Possible Duplicate:
How to copy to the clipboard in JavaScript?
I have an html markup
thats echoed and is wrapped in a textarea
and its also a read only like so:
<textarea rows="4" cols="70" readonly="readonly">text with markup goes here.</textarea>
how do I do it that when a user clicked on the read-only text area contents, it will be automatically selected and copied to the clipboard and with a notification that its already copied?
PHP is server-side so you won't be able to do anything to interact with the client. In terms of client-side then javascript can do something. The bad news is that Firefox doesn't allow this any more as far as I recall (I remember seeing some debate around it regarding TinyMCE's clipboard functionality) so the odds are you'll need something mixing flash and js to get a fully cross-browser solution.
Might I thus suggest http://code.google.com/p/zeroclipboard/ as a possibility?
为什么你想要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>
链接地址: http://www.djcxy.com/p/19724.html