0

I'm trying to use Zeroclipboard to copy stuff to the clipboard, but it doesn't seem to be working. My code:

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>

What's wrong with this? Thansk!

4

2 回答 2

4

一些东西。

首先,你的括号有点偏离。它应该是:

jQuery(document).ready(function(){

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

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

但这不会解决你的问题。

请参阅ZeroClipBoard 说明

您需要将 Flash 电影“粘合”或链接到页面上的 dom 元素。这是存储复制文本的位置。然后,您不能将 jQuery 用于单击事件(或者,如果可以,我误解了文档),但您可以将 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。

希望有帮助。

于 2010-05-13T10:50:26.673 回答
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);
    }
于 2013-01-23T11:12:33.377 回答