使用jquery或javascript将文本复制到剪贴板 [英] copy text to clipboard with jquery or javascript

查看:105
本文介绍了使用jquery或javascript将文本复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Google使用jquery或javascript将一些文本复制到剪贴板.我知道那里

how to copy some text to the clipboard with jquery or javascript, from google. i know there

是名为 zeroclipboard 的pludgin, http://code.google.com /p/zeroclipboard/可以使用

is a pludgin named zeroclipboard http://code.google.com/p/zeroclipboard/ can do this with

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

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

but when i tested it on my site. i set it to copy text optionally . it can't work.

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

它总是复制所有值.即使我取消选中某些复选框.可能是该值未更改.但是当我alter()变量时,该值确定.如何纠正呢?谢谢.

it always copys all the values. even i uncheck some check box. may be the value doesn't be changed. but when i alter() the variable, the value is ok. how to correct it? thank you.

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

i want it can copy the checked box value. if the box unchecked, then don't copy its value.

推荐答案

花点时间找出答案(没有对.swf的正确引用),但这是一个完整的工作示例(已测试IE 8和Firefox 4)

Took me a while to figure this out (didn't have the correct references to the .swf), but here is a complete working example (tested 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>   

原始:

您的按钮没有点击事件.

You don't have a click event for your button.

您需要添加以下内容:

    var clip = new ZeroClipboard.Client();
    $("#copyvalue").click(function(){
        var text = '';
        $('input.forminput:checked').each(function() {
            text += $(this).val() + "\n";
        });
        //alert(text);
        clip.setText(text);
    });

这篇关于使用jquery或javascript将文本复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆