3

如何使用 google 的 jquery 或 javascript 将一些文本复制到剪贴板。我知道那里

是一个名为zeroclipboard http://code.google.com/p/zeroclipboard/的插件可以用

跨浏览器。说明链接http://code.google.com/p/zeroclipboard/wiki/Instructions

但是当我在我的网站上测试它时。我将其设置为可选地复制文本。它行不通。

我的测试链接是http://xanlz.com/copy/1.html

它总是复制所有的值。即使我取消选中某些复选框。可能是值没有改变。但是当我alter()是变量时,值就可以了。如何纠正它?谢谢你。

我希望它可以复制复选框值。如果未选中该框,则不要复制其值。

4

2 回答 2

1

编辑 :

我花了一段时间才弄清楚(没有正确引用 .swf),但这是一个完整的工作示例(经过测试的 IE 8 和 Firefox 4)

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
    <script type="text/javascript" src="http://zeroclipboard.googlecode.com/svn/trunk/ZeroClipboard.js" ></script>
    <script type="text/javascript" >
        $(document).ready(function(){
            var checkall = $('#checkall');
            var boxes = $('input[type="checkbox"]').not(checkall);
            checkall.click(function () {
                boxes.attr('checked', this.checked);
            });
            boxes.change(function() {
                checkall[0].checked = this.checked && boxes.filter(':checked').length === boxes.length;
            });

            ZeroClipboard.setMoviePath('http://zeroclipboard.googlecode.com/svn-history/r10/trunk/ZeroClipboard.swf');
            var clip = new ZeroClipboard.Client();

            clip.glue("copyvalue");
            clip.addEventListener( 'onMouseOver', function(){
                var text = ' '; //Make this a space since we can't copy nothing...
                $('input.forminput:checked').each(function() {
                    text += $(this).val() + "\n";
                });
                clip.setText(text);
            });
        }) ;
    </script>
  </head>
  <body>
    <input class="forminput" type="checkbox" value="test one" checked="checked" name="VD1">
    <br>
    <input class="forminput" type="checkbox" value="test two" checked="checked" name="VD2">
    <br>
    <input class="forminput" type="checkbox" value="test three" checked="checked" name="VD3">
    <br>
    <input class="forminput" type="checkbox" value="test four" checked="checked" name="VD4">
    <br>
    <input class="forminput" type="checkbox" value="test five" checked="checked" name="VD5">
    <br>
    <input id="checkall" type="checkbox" checked="checked" name="checkall">
    <input id="copyvalue" class="button" type="button" value="copy test">
  </body>
</html>   

原来的:

您的按钮没有点击事件。

您需要添加以下内容:

    var clip = new ZeroClipboard.Client();
    $("#copyvalue").click(function(){
        var text = '';
        $('input.forminput:checked').each(function() {
            text += $(this).val() + "\n";
        });
        //alert(text);
        clip.setText(text);
    });
于 2011-06-16T14:56:31.253 回答
0

如果您不喜欢使用(或不允许)依赖闪存(我个人不会),我认为您最好创建一个文本区域,其中包含预先选择的文本(或序列化数据)指令用户复制并稍后粘贴数据。

您可以通过嗅探操作系统来使指令感觉更本地化,并就如何复制和粘贴给出不同的指令(例如 ctrl+c 用于 windows/linux 或 ⌘-C 用于 mac)。

也许没有那么性感,但更万无一失......

于 2011-06-17T14:16:53.583 回答