【问题标题】:Zeroclipboard multiple elementsZeroclipboard 多个元素
【发布时间】:2010-01-28 08:49:14
【问题描述】:

在我的代码中创建多个 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 个弹出窗口一样触发,要么根本不触发。

附:我尝试从 How to load an Ajax response into clipboard using jQuery and ZeroClipboard? 调整解决方案,而不是 ajax 调用,只需复制到剪贴板并在完成午餐时弹出一个弹出窗口......正如我所说的那样对我不起作用。

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

【问题讨论】:

    标签: jquery click popupwindow zeroclipboard


    【解决方案1】:

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

    $("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);
      });
    });
    

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

    【讨论】:

    • 感谢@Andrei 的分享!它有帮助! “clip.glue(这个);”成功了!
    【解决方案2】:

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

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

    这是一个sn-p:

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

    确保更改 swf 的路径。

    【讨论】:

      【解决方案3】:

      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-30
        • 2016-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多