简体   繁体   English

剪贴板.js:JavaScript API上发生非法构造函数错误

[英]clipboard.js: Illegal Constructor errors occuring on JavaScript API

I'm trying to run a Copy URL event using clipboard.js. 我正在尝试使用剪贴板.js运行“复制URL”事件。 I have it installed on my server and the reference to clipboard.js is there in my code. 我已经将其安装在服务器上,并且在代码中也有对剪贴板.js的引用。 So I have this in my footer: 所以我在页脚中有这个:

<script type="text/javascript">
     var url = document.location.href;

    new Clipboard('.btn', {
        text: function() {
    return url;
  }
});
</script>

And this simply for my button: 这只是我的按钮:

<button class="btn">Copy</button>

Simple. 简单。 And there's an example on SO that does work: Copy URL from browser using clipboard.js 在SO上有一个可行的示例: 使用剪贴板.js从浏览器复制URL

But mine is throwing an Illegal Constructor error on my script and I'm really puzzled as to why. 但是我的脚本上抛出了一个非法的构造函数错误,我真的很困惑为什么。 Am I forgetting something that's causing this error to appear? 我是否忘记了导致此错误出现的问题?

Here's the Stack example: Copy URL from browser using clipboard.js 这是Stack的示例: 使用Clipboard.js从浏览器复制URL

Here's what I got: https://dadventuresla.com/copy-link-test/ 这是我得到的: https : //dadventuresla.com/copy-link-test/

as per https://clipboardjs.com/ it should be ClipboardJS 根据https://clipboardjs.com/,它应该是ClipboardJS

<script type="text/javascript">
     var url = document.location.href;

    new ClipboardJS('.btn', {
        text: function() {
    return url;
  }
});
</script>

You have a typo - looking at the Clipboard docs , it shows you need to use ClipboardJS not Clipboard : 您有一个错字-查看剪贴板文档 ,它表明您需要使用ClipboardJS而不是Clipboard

<script type="text/javascript">
    var url = document.location.href;
    new ClipboardJS(".btn", {
        text: function() {
            return url;
        }
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM