使用 ZeroClipboard 将 URL 复制到剪贴板

发布于 2025-01-01 03:42:16 字数 1754 浏览 1 评论 0原文

我正在尝试利用 ZeroClipboard (http://code.google.com/p/zeroclipboard/wiki/Instructions) 将当前 URL 复制到用户的剪贴板。我知道我在这里遗漏了一些东西,但我在控制台中没有收到任何类型的错误,也没有工作:

JavaScript

<script src="/js/zero-clipboard.js"></script>
<script>
        var clip = null;
        ZeroClipboard.setMoviePath( '/ZeroClipboard10.swf' );
        function $(id) { return document.getElementById(id); }
        function init() {
            clip = new ZeroClipboard.Client();
            clip.setHandCursor( true );

            clip.addEventListener('load', function (client) {
                debugstr("Flash movie loaded and ready.");
            });

            clip.addEventListener('mouseOver', function (client) {
            // update the text on mouse over
                clip.setText( $('#copyURL').href );
            });

            clip.addEventListener('complete', function (client, text) {
                debugstr("Copied text to clipboard: " + text );
            });

clip.glue( 'copyURL', 'copyURLContainer' );
        }

        function debugstr(msg) {
            var p = document.createElement('p');
            p.innerHTML = msg;
            $('d_debug').appendChild(p);
        }
</script>

HTML:

<div id="copyURLContainer">
    <a id="copyURL" href="javascript:window.location">COPY URL</a>
</div>

任何想法我在代码中遗漏了什么?

编辑:我还尝试将clip.addEventListener 设置为window.location。那也没用。我可以把 $('#copyURL') 部分取出来吗?

clip.addEventListener('mouseOver', function (client) {
            // update the text on mouse over
                clip.setText( $('#copyURL').window.location );
            });

这个我还没想明白有人对我缺少的东西有什么想法吗?

I'm trying to utilize ZeroClipboard (http://code.google.com/p/zeroclipboard/wiki/Instructions) to copy the current URL to the user's clipboard. I know I'm missing something here, but I am not getting any kind of error in the console, nor is it working as of yet:

JavaScript

<script src="/js/zero-clipboard.js"></script>
<script>
        var clip = null;
        ZeroClipboard.setMoviePath( '/ZeroClipboard10.swf' );
        function $(id) { return document.getElementById(id); }
        function init() {
            clip = new ZeroClipboard.Client();
            clip.setHandCursor( true );

            clip.addEventListener('load', function (client) {
                debugstr("Flash movie loaded and ready.");
            });

            clip.addEventListener('mouseOver', function (client) {
            // update the text on mouse over
                clip.setText( $('#copyURL').href );
            });

            clip.addEventListener('complete', function (client, text) {
                debugstr("Copied text to clipboard: " + text );
            });

clip.glue( 'copyURL', 'copyURLContainer' );
        }

        function debugstr(msg) {
            var p = document.createElement('p');
            p.innerHTML = msg;
            $('d_debug').appendChild(p);
        }
</script>

HTML:

<div id="copyURLContainer">
    <a id="copyURL" href="javascript:window.location">COPY URL</a>
</div>

Any ideas what I'm missing in my code?

Edit: I also tried making the the clip.addEventListener set to window.location. That didn't work either. Could I pull the $('#copyURL') bit out?

clip.addEventListener('mouseOver', function (client) {
            // update the text on mouse over
                clip.setText( $('#copyURL').window.location );
            });

I still haven't figured this one out. Anyone have any ideas on what I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2025-01-08 03:42:16

事实证明,当将clip.setText设置为window.location时,它传递了一个对象。必须开始一个空字符串才能正确通过。现在正在工作。

clip.addEventListener('mouseOver', function (client) {
            // update the text on mouse over
                clip.setText(""+window.location);
            });

Turns out when setting clip.setText to window.location, it was passing an object. Had to start an empty string in order for it to pass properly. It's working now.

clip.addEventListener('mouseOver', function (client) {
            // update the text on mouse over
                clip.setText(""+window.location);
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文