3

我正在尝试使用 Zeroclipboard http://code.google.com/p/zeroclipboard/将内容复制到剪贴板并在鼠标悬停在闪光灯上时添加工具提示。但它似乎没有工作。

我的html代码:

<div rel="<?php echo $url;?>" class="cp-code">copied code</div>
<div class="test" style="display: none; border: 1px solid #ccc; padding: 8px;">click copy,test,test</div>

我的 js 代码:我添加了 jquery 库。

ZeroClipboard.setMoviePath("http://example.com/js/ZeroClipboard.swf");
var clip = null;
var url = '';

function init() {
    clip = new ZeroClipboard.Client();
    clip.setHandCursor( true );

    $('.cp-code').mouseover( function() {
        clip.setText(this.innerHTML);
             $('test').style.display = 'block';
        if (clip.div) {
            clip.receiveEvent('mouseout', null);
            clip.reposition(this);
        } else {
                        clip.glue(this);
                }
        clip.receiveEvent('mouseover', null);
        url = $(this).attr('rel');
    });

    clip.addEventListener('mouseUp', function(client) {
        window.open(url);
    });
  clip.addEventListener('mouseOut', function (client) {
 $('test').style.display = 'none';
   });

    }

$(document).ready(function() {
    init();
});
4

1 回答 1

1

为什么你希望它在鼠标悬停时发生?我不确定 ZeroClipboard 是否支持。

当我第一次使用 ZeroClipboard 时,我花了一些时间才弄清楚这一点,因为它的实现与正常情况有点不同。但是,您不能只调用clip.setText。您必须将剪辑实现“粘合”到控件上。而且你也不能使用 jQuery 对象,你必须把它粘到实际的 DOM 对象上。

因此,例如:

var cpCode = $('.cp-code');
cpCode.each(function()
{
    clip = new ZeroClipboard.Client(); //you can set the movie path here too
    clip.glue($(this)[0]); // The [0] accesses the actual DOM object rather than the jQuery object
    clip.setText($(this).html();
});

所以现在当你点击元素时,文本将被复制。我看到你在你的例子中做了其他一些事情,但无论如何,我认为你缺少的元素是将 DOM 对象粘合到剪辑实例,而不是调用剪辑.setText 在您的 jQuery 鼠标悬停事件上。

于 2011-06-28T16:28:42.370 回答