javascript ZeroClipboard 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13089563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:47:59  来源:igfitidea点击:

ZeroClipboard just isn't working

javascriptgoogle-chrometypeerrorzeroclipboardreferenceerror

提问by mythofechelon

Originally, this post was regarding my attempts to inject ZeroClipboard into web pages by and for use by my Chrome extension, but I've dumbed the scenario down and down and down in a seemingly futile attempt to identify the issue and I stillcan't get it to work.

最初,这篇文章是关于我尝试通过 Chrome 扩展程序将 ZeroClipboard 注入网页并供其使用,但我已经一次又一次地简化了这个场景,以看似徒劳的尝试来识别问题,但我仍然不能让它工作。

I'm even having difficulty getting the actual, documented "Minimal Example" on ZeroClipboard's own GitHub to work (admittedly, I've modded the source to actually be HTML5-valid, but the exact original didn't work either). Even test.html, which is included in the tar.gzarchive, doesn't work!

我什至很难让ZeroClipboard 自己的 GitHub 上实际的、记录在案的“最小示例”工作(不可否认,我已经修改了源代码实际上是 HTML5 有效的,但确切的原始代码也不起作用)。Even test.html,包含在tar.gz存档中,不起作用!

"Minimal Example": Code

“最小示例”:代码

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <input type="button" id="d_clip_button" data-clipboard-text="Copy Me!" value="Copy To Clipboard" />
        <script src="ZeroClipboard.js"></script>
        <script>
            var clip = new ZeroClipboard( document.getElementById('d_clip_button') );
        </script>
    </body>
</html>

"Minimal Example": Console Output

“最小示例”:控制台输出

Uncaught TypeError: object is not a function  index.html:11

Info

信息

  • The entire contents of the zeroclipboard-1.0.7.tar.gzarchiveare in the same directory as index.html.
  • The ZeroClipboard.jsfile isn't corrupted / incomplete and is being loaded correctly.
  • I'm using Chrome v24.0.1312.52
  • zeroclipboard-1.0.7.tar.gz存档的全部内容与index.html.
  • ZeroClipboard.js文件没有损坏/不完整,并且正在正确加载。
  • 我正在使用 Chrome v24.0.1312.52

 

 

Either I'm missing something really, really obvious here or ZeroClipboard's documentation / functionality is abysmal.

要么我在这里遗漏了一些非常非常明显的东西,要么 ZeroClipboard 的文档/功能非常糟糕。

采纳答案by Uwe Keim

What I've just found out:

我刚刚发现的:

That didn't match. I've got a similar error than the original poster of this question.

那不匹配。我有一个与这个问题的原始海报类似的错误。

After I used the example from GitHubI've succeeded.

在使用GitHub 上的示例后,我成功了。

Conclusion:

结论:

Either use both the example anddownload from Google Code or (which I would prefer), both the example anddownload from GitHub.

要么使用示例从 Google Code 下载,要么(我更喜欢)使用示例从 GitHub 下载。

回答by mLar

you will need to specify the path to the swf file :

您需要指定 swf 文件的路径:

 var clip = new ZeroClipboard( document.getElementById('d_clip_button'),{moviePath: "/path/ZeroClipboard.swf"} );

回答by user3559718

Try the 2.2.1-dev version: http://datatables.net/download/build/dataTables.tableTools.nightly.js?_=60133663e907c73303e914416ea258d8I think it should be resolve there. I would also point out that new $.fn.dataTable.TableTools(...) is now the preferred form. The TableTools global is still in 2.2.x, but will be removed in 2.3+.

尝试 2.2.1-dev 版本:http: //datatables.net/download/build/dataTables.tableTools.nightly.js?_ = 60133663e907c73303e914416ea258d8我认为它应该在那里解决。我还要指出 new $.fn.dataTable.TableTools(...) 现在是首选形式。TableTools 全局仍在 2.2.x 中,但将在 2.3+ 中删除。

回答by Nicolas BADIA

For me, the problem was that the variable module was already defined by the framework I use and not meant to be use like ZeroClipboard do. In fact, if you look at the end of the ZeroClipboard.js file, here is what you can see:

对我来说,问题是变量模块已经由我使用的框架定义,而不是像 ZeroClipboard 那样使用。事实上,如果您查看 ZeroClipboard.js 文件的末尾,您可以看到以下内容:

  if (typeof module !== "undefined") {
    module.exports = ZeroClipboard;
  } else if (typeof define === "function" && define.amd) {
    define(function() {
      return ZeroClipboard;
    });
  } else {
    window.ZeroClipboard = ZeroClipboard;
  }

I just replaced this by the following code to fix the issue:

我只是用以下代码替换了它来解决这个问题:

window.ZeroClipboard = ZeroClipboard;

回答by Jay

For loading an external script, inserting a SCRIPTelement into the page body will notimmediately load and execute the script. The appendChildwill return immediately, and the script will be loaded afterthe current script execution returns from all functions and goes into idle state.

对于加载外部脚本,将SCRIPT元素插入页面正文不会立即加载和执行脚本。在appendChild将返回立即,脚本将被加载后,所有功能的当前脚本执行返回并进入闲置状态。

For this, the SCRIPTelement's onloadproperty can be used to execute a code once the external script is loaded.

为此,一旦加载了外部脚本,SCRIPT元素的onload属性就可用于执行代码。

function inject_zeroClipboard(){
    var path_Root_zeroClipboard = chrome.extension.getURL("plugins/zeroClipboard");
    var element_Head = document.getElementsByTagName('head')[0];
    var element_zeroClipboard = document.createElement("script");
    element_zeroClipboard.src = path_Root_zeroClipboard + "/ZeroClipboard.js";

    element_zeroClipboard.onload = function() {
        ZeroClipboard.setMoviePath(path_Root_zeroClipboard + "/ZeroClipboard10.swf");
        var clip = new ZeroClipboard.Client();
        clip.glue("element_Example");
        clip.addEventListener("click", function(){
            clip.setText(data_Example);
        });
    };

    element_Head.appendChild(element_zeroClipboard);
}