zeroclipboard fire click event via jquery

2019-05-17 07:08发布

问题:

ZeroClipboard is a javascript + flash script that allows the browser to put text into the clipboard, it puts a transparent flash over the selected element , and when you click it you can insert text into the clipboard, this works, no problem, but i want to make it automatic. Onload put a string in the clipboard, for the user to be able to paste it latter. Some code main.js

$('document').ready(function() {
ZeroClipboard.setMoviePath("http://url/to/ZeroClipboard.swf");
  var clip=new ZeroClipboard.Client();
 clip.on( 'load', function(client) {
   clip.glue('#redirlink');
} );
});

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" >
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="main.js"></script>

    <title>TEST</title>
  </head>
  <body>


      <div >
     <a  id="redirlink" data-clipboard-text="abcd" href="http://alabala.com" >Click HERE</a>

    </div>

  </body>
</html>

ok now the question is how can i simulate a click event on the the zeroClipboard puts? i have tried with , $('#obkect_id").click(); it doesn't work (the event fires, but it has no effect on the clipboard, but when i click with the mouse, it works). Is there any way i can achieve that?

回答1:

The newest versions of ZeroClipboard have done away with Client(). Hence the code changes a bit too. Download the latest version from Github here

Also then say you want to update/put some string when page/element loads. Say on clicking the 'element' having id '#id', the clipboard needs to be loaded with some text

<script>
var str = 'Some text';
$('element').attr('data-clipboard-text','str');
ZeroClipboard.setDefaults({moviePath:'http://...../ZeroClipboard.swf'});
var clip = new ZeroClipboard($('#id'));
clip.on('complete',function(client, args){
alert('Copied');
});
</script>

Hope this helps



回答2:

Unfortunately you can't simulate a click on the flash object, and have it setData in the clipboard http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/desktop/Clipboard.html#. It's a security precaution by Adobe.