admin管理员组

文章数量:1313944

I'm using to copy some text to the clipboard and that code is working just fine. It uses flash to create a crossbrowser solution and it is based on ZeroClipboard, which seems to be considered to be the best working solution at the moment.

However I would like to have multiple copy to clipboard buttons or links on my page. Here is an example.

/

This code works, it copies the text of the coupon code to the clipboard and opens up a new page with the correct link. How can I use that code on other links without having to duplicate it for each and every link / id.

Using just the class

$(function() {
$('.copy').zclip({
    path: '.swf',
    copy: $(this).text(),
    afterCopy: function() {
        window.open($(this).attr('href'));
    }
});

});

doesn't work: as you can see here: / if you remove the afterCopy function you'll see that $(this).text() will return the whole page instead of just the text between the link tag.

doing something like this

$(function() {
$('a.copy', this).zclip({
    path: '.swf',
    copy: $('a.copy', this).text(),

});

});

slightly improves upon it but returns all text between the link tag as you can see here. /

I'm using http://www.steamdev./zclip/#usage to copy some text to the clipboard and that code is working just fine. It uses flash to create a crossbrowser solution and it is based on ZeroClipboard, which seems to be considered to be the best working solution at the moment.

However I would like to have multiple copy to clipboard buttons or links on my page. Here is an example.

http://jsfiddle/stofke/TB23d/

This code works, it copies the text of the coupon code to the clipboard and opens up a new page with the correct link. How can I use that code on other links without having to duplicate it for each and every link / id.

Using just the class

$(function() {
$('.copy').zclip({
    path: 'http://shopsheep./js/ZeroClipboard.swf',
    copy: $(this).text(),
    afterCopy: function() {
        window.open($(this).attr('href'));
    }
});

});

doesn't work: as you can see here: http://jsfiddle/stofke/EAZYW/ if you remove the afterCopy function you'll see that $(this).text() will return the whole page instead of just the text between the link tag.

doing something like this

$(function() {
$('a.copy', this).zclip({
    path: 'http://shopsheep./js/ZeroClipboard.swf',
    copy: $('a.copy', this).text(),

});

});

slightly improves upon it but returns all text between the link tag as you can see here. http://jsfiddle/stofke/hAh3j/

Share Improve this question edited Mar 31, 2011 at 7:09 Stofke asked Mar 31, 2011 at 6:40 StofkeStofke 2,9682 gold badges24 silver badges29 bronze badges 1
  • I’m voting to close and delete this question because the code on which it is based no longer exists. I cannot delete my own answer – mplungjan Commented May 12, 2020 at 6:28
Add a ment  | 

4 Answers 4

Reset to default 3

UPDATE: This no longer works but I cannot delete the post

This seems to work - someone might be able to make it more elegant

http://jsfiddle/5nLw6/7/

$(function() {
    $('.copy').each(function() {
        var linkId = $(this).attr("id");
        $(this).zclip({
        path: 'http://shopsheep./js/ZeroClipboard.swf',
        copy: $("#"+linkId).text(),
        afterCopy: function() {
            window.open($('#'+linkId).attr('href'));
        }
    });
  });
});

I actually discovered that using ZeroClipboard directly is just as easy, I just added this code in case someone wants a solution without using zclip.

ZeroClipboard.setMoviePath('http://shopsheep./js/ZeroClipboard.swf');
$(document).ready(function() {
    $(".copy").each(function(i) {
        var clip = new ZeroClipboard.Client();
        var myTextToCopy = $(this).text();
        var myTextUrl = $(this).attr('href');
        clip.setText(myTextToCopy);
        clip.addEventListener('plete', function(client, text) {
            window.open(myTextUrl);
        });
        clip.glue($(this).attr("id"));
    });
});

http://jsfiddle/stofke/JxMbd/

This is what we follow in Oodles Technologies.

To use zero copy to clipboard you need two files 1 . ZeroClipboard.js
2 .ZeroClipboard.swf both file can be download from here

<html>
<head>
    <script src =”../ZeroClipboard.js”></script>
    <script >
     // configure ZeroClipboard first
     ZeroClipboard.config( { moviePath : /path/swffile/ZeroClipboard.swf } );

     // initialize constructor
    var client = new ZeroClipboard($(“#elementid”));

    /* elementid is the element on which click , the data will copy  to clipboard. you can also pass multiple elements, it use jquery selector */
        </script>
<body>
<input type=”text”  id =”targetid”></button>
<button  id =”elementid” data-clipboard-text ='data for copy’ >copy</button>
</body>
</head>
<html>

ZeroClipboard automatically copy the value of data-clipboard-text attribute when event accur on element pass to ZeroClipboard's constructor

Light weight jQuery solution... re-use class to copy text from any element.

$(document).on('click', '.copytoclipboard', function(e) {
  if($("#holdtext").length < 1)
    $("body").append('<textarea id="holdtext" style="height:0;width:0;border:0;outline:0;resize:none;"></textarea>');
  $("#holdtext").val($(this).text()).select();
  document.execCommand("Copy");
});

本文标签: Jqueryjavascript copy to clipboardStack Overflow