ZeroClipboard有问题

时间:2010-05-13 09:18:53

标签: jquery zeroclipboard

我正在尝试使用Zeroclipboard将内容复制到剪贴板,但它似乎不起作用。我的代码:

HTML:

<textarea name="texter" id="texter"></textarea>
<input type="button" value="Copy to clipboard" id="copy-button" />

使用Javascript:

<script type="text/javascript">
jQuery(document).ready(function(){

  var clip = new ZeroClipboard.Client();
  clip.setText('');  

   jQuery('#copy-button').click(function(){
  clip.setText(jQuery('#texter').val());
 }


});
</script>

这有什么问题? Thansk!

2 个答案:

答案 0 :(得分:4)

一些事情。

首先,您的括号稍微偏离。 它应该是:

jQuery(document).ready(function(){

  var clip = new ZeroClipboard.Client();
  clip.setText('');  

   jQuery('#copy-button').click(function(){
  clip.setText(jQuery('#texter').val());
 });
});

但这不会解决你的问题。

请参阅ZeroClipBoard instructions

您需要“粘贴”或将Flash影片链接到页面上的dom元素。这是存储复制文本的位置。然后,您不能将jQuery用于click事件(或者如果可以,我会误解文档),但您可以将mousedown事件注册到按钮并将其绑定到剪辑。

将此应用于您的代码。

    <script type="text/javascript">
        $(document).ready(function () {
            var clip = new ZeroClipboard.Client();

            clip.setText(''); // will be set later on mouseDown

            clip.addEventListener('mouseDown', function (client) {
                // set text to copy here
                clip.setText(jQuery('#texter').val());

                // alert("mouse down"); 
            });

            clip.glue('copy-button');
        });
</script>

这应该有用。

你可以在没有jQuery的情况下完全使用这个例子,但是在文档中准备它是一个很好的紧凑的地方,以确保它只在DOM准备好后执行。并且还使用jQuery代替getElementById。

希望有所帮助。

答案 1 :(得分:0)

<!-- <script type="text/javascript" src="http://davidwalsh.name/demo/ZeroClipboard.js"></script> -->
    function copyText(fieldName,buttonName){
        var fieldNameTemp =fieldName;
        var buttonNameTemp =buttonName;
        var val = "";
        try{
            val = navigator.userAgent.toLowerCase();
        }catch(e){}
        var swfurl = "js/ZeroClipboard.swf";
        setTimeout(function () {
            ZeroClipboard.setMoviePath(swfurl);
            var clip = new ZeroClipboard.Client();
            clip.addEventListener('mousedown', function () {
                clip.setText(document.getElementById(fieldNameTemp).value);
            });
            clip.addEventListener('complete', function (client, text) {
                try{
                    if(val.indexOf("opera") > -1 || val.indexOf("msie") > -1 || val.indexOf("safari") > -1 || val.indexOf("chrome") > -1){
                        alert('Your text has been copied');
                    }
                }catch(e){
                    alert('Please alert: not use on fireFox');
                }
            });
            clip.glue(buttonNameTemp);
        }, 2000);
    }