ZeroClipboard-将文本复制到剪贴板不起作用

ZeroClipboard - copy text to clipboard not working

本文关键字:剪贴板 不起作用 复制 文本 ZeroClipboard-      更新时间:2023-09-26

我想尝试这个DEMO通过使用ZeroClipboard将文本复制到剪贴板。我有一个本地文件夹,里面有index.html、ZeroClipboard.js和ZeroClipbard.swf。但它不起作用:

<html>
<body>
    <script type="text/javascript" src="ZeroClipboard.js"></script>
    <script type="text/javascript">
        //set path
        ZeroClipboard.setMoviePath('ZeroClipboard.swf');
        //create client
        var clip = new ZeroClipboard.Client();
        //event
        clip.addEventListener('mousedown', function () {
            clip.setText(document.getElementById('box-content').value);
        });
        clip.addEventListener('complete', function (client, text) {
            alert('copied: ' + text);
        });
        //glue it to the button
        clip.glue('copy');
    </script>
    <textarea name="box-content" id="box-content" rows="5" cols="70">
        The David Walsh Blog is the best blog around!  MooTools FTW!
    </textarea>
    <br />
    <br />
    <p>
        <input type="button" id="copy" name="copy" value="Copy to Clipboard" />
    </p>
</body>
</html>

您必须运行服务器。因为指向您网站的链接必须包含http或https。这是因为adobeflash的安全设置

确保在复制按钮中生成iframe标记。初始化Zeroclipboard.Client().后缺少一行

clip.setHandCursor(true)

 <script type="text/javascript">
    //set path
    ZeroClipboard.setMoviePath('ZeroClipboard.swf');
    //create client
    var clip = new ZeroClipboard.Client();
    //event
    clip.setHandCursor( true );          

    clip.addEventListener('mousedown', function () {
        clip.setText(document.getElementById('box-content').value);
    });
    clip.addEventListener('complete', function (client, text) {
        alert('copied: ' + text);
    });
    //glue it to the button
    clip.glue('copy');
    </script>

我希望它对你有用。

确保ZeroClipboard.jsZeroClipboard.swf都可用。要验证这一点,您可以打开控制台并查找错误。还要确保已安装并启用Adobe Flash。

在所有其他情况下,该示例都应该有效。

在定义元素box-content之前,您正在调用javascript。

clip.addEventListener('mousedown', function () {
   clip.setText(document.getElementById('box-content').value);
});

试试这个:

<html>
<body>
    <textarea name="box-content" id="box-content" rows="5" cols="70">
        The David Walsh Blog is the best blog around!  MooTools FTW!
    </textarea>
    <br />
    <br />
    <p>
        <input type="button" id="copy" name="copy" value="Copy to Clipboard" />
    </p>
    <script type="text/javascript" src="ZeroClipboard.js"></script>
    <script type="text/javascript">
        //set path
        ZeroClipboard.setMoviePath('ZeroClipboard.swf');
        //create client
        var clip = new ZeroClipboard.Client();
        //event
        clip.addEventListener('mousedown', function () {
            clip.setText(document.getElementById('box-content').value);
        });
        clip.addEventListener('complete', function (client, text) {
            alert('copied: ' + text);
        });
        //glue it to the button
        clip.glue('copy');
    </script>
</body>
</html>