3

我在代码中创建多个 Zeroclipboard 实例时遇到问题,每个实例在调用后都会启动一个弹出窗口。

<a class="xxx" href="popup.url.php" ><span >FRSDE3RD</a>
<a class="xxx" href="popup.url2.php" ><span >FRSDE3RD2</a>
<a class="xxx" href="popup.url3.php" ><span >FRSDE3RD3</a>
$(document).ready(function(){
    ZeroClipboard.setMoviePath( 'path/to/swf/ZeroClipboard.swf' );
    // setup single ZeroClipboard object for all our elements
    clip = new ZeroClipboard.Client();
    clip.setHandCursor( true );

    // assign a common mouseover function for all elements using jQuery
    $('a.xxx').mouseover( function() {
        // set the clip text to our innerHTML
        var url = $(this).attr("href");
        var code = $(this).children('span').html();
        clip.setText( $(this).children('span').html() );//this.innerHTML );

        clip.glue(this);
        clip.addEventListener('onMouseDown', function(){
            clip.reposition(this);
            clip.setText( code );
        });

        clip.addEventListener('onComplete', function(){ 
            clip.reposition(this);
            popUp(url);
        }); 


    });
});

function popUp(URL)
{
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=1024,height=768,left = 328,top = 141');");
}

我成功地生成了复制到剪贴板的功能,但是如果我使用 onMouseUp、onComplete 事件来触发弹出功能,它要么像 4-5 个弹出窗口一样触发,要么根本不触发。

PS 我试图从如何使用 jQuery 和 ZeroClipboard 将 Ajax 响应加载到剪贴板?而不是 ajax 调用只是复制到剪贴板并在完成午餐时弹出一个弹出窗口......正如我所说的那样对我不起作用。

在启用 flashblocker 时我还发现,每次我翻转一个 CODE 标签时,都会在同一个位置创建一个新的 flash,所以这可能是我点击它时弹出 3-4 个弹出窗口的解释。如果我翻转更多,就会出现更多弹出窗口。有没有办法阻止闪光灯在同一地点创建或在推出时销毁?

4

3 回答 3

14

经过更多研究后,我找到了解决此问题的方法:

$("a.xxx").each(function() {
  //Create a new clipboard client
  var clip = new ZeroClipboard.Client();
  clip.setHandCursor( true );

  //Glue the clipboard client to the last td in each row
  clip.glue(this);

  var url = $(this).attr("href");
  //Grab the text from the parent row of the icon
  var code = $(this).children('span').html();    
  clip.setText(code);

  //Add a complete event to let the user know the text was copied
  clip.addEventListener('complete', function(client, text) {
    //alert("Copied text to clipboard:\n" + text);
    popUp(url);
  });
});

如果其他人会遇到这个问题,这就是解决方案。

于 2010-01-29T09:26:55.437 回答
0

尝试使用http://www.steamdev.com/zclip/它允许您直接访问 jquery,并且您可以在 return 语句中使用自己的逻辑。

包括 jquery.zclip.js 下载并保存 ZeroClipboard.swf

这是一个片段:

$(".class-to-copy").zclip({
    path: "assets/js/ZeroClipboard.swf",
    copy: function(){
        return $(this).attr("data-attribute-with-text-to-copy");
    }
});

确保更改 swf 的路径。

于 2014-10-05T08:37:02.197 回答
0

Andrei C 的回答已经过时了。只需按以下方式进行。

<a id="test1" class="test" href="#"  data-clipboard-text="1">111</a>
<a id="test2" class="test" href="#"  data-clipboard-text="2">111</a>
<a id="test3" class="test" href="#"  data-clipboard-text="3">111</a>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="dist1/ZeroClipboard.js"></script>
<script>
var client = new ZeroClipboard( );
client.clip($(".test"));

client.on( "ready", function( readyEvent ) {
  client.on( "aftercopy", function( event ) {
    alert("Copied text to clipboard: " + event.data["text/plain"] );
  } );
});

</script>
于 2016-01-21T07:12:31.820 回答