admin管理员组

文章数量:1425697

Trying to make a link that will copy some text, and what I was wondering (I have read some articles but they were for when people input things) if there was a way to just put any random text into the clipboard? I wanted to do this using JavaScript to do the actual copying and use PHP to get the actual data that I want to copy, I dont know that much JavaScript so keep that in mind, Thanks.

Trying to make a link that will copy some text, and what I was wondering (I have read some articles but they were for when people input things) if there was a way to just put any random text into the clipboard? I wanted to do this using JavaScript to do the actual copying and use PHP to get the actual data that I want to copy, I dont know that much JavaScript so keep that in mind, Thanks.

Share Improve this question asked Jan 19, 2016 at 14:21 Jack HalesJack Hales 1,6641 gold badge27 silver badges54 bronze badges 1
  • Nothing will beat Zero Clipboard: stackoverflow./questions/14432838/… – Hanky Panky Commented Jan 19, 2016 at 14:27
Add a ment  | 

2 Answers 2

Reset to default 3

This is a recurrent question and it is a lot of "solutions" but mostly you will end up using flash if you want full platform support.

If that is not the case, you can use libraries like clipboard.js, and there's a lot more out there, just look for them.

HTML

<div id="copy-text">Some Text</div>
<button onclick="copyToClipboard('#copy-text')" type="button" >Copy</button>

JS

function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
}

本文标签: Copy text to clipboard using JavaScriptPHPStack Overflow