最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use clipboard.js - Stack Overflow

programmeradmin2浏览0评论

As you may surmise from this question, I'm not very good with javascript and am trying to get clipboard.js (/) working but can't. I followed instructions by copying clipboard.min.js into the scripts folder and then referenced it in my html file. Then I copied their button (and modified it a bit like this:

<button class="btn" id="test" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
    Copy to clipboard
</button>

In their setup instructions, they say this:

"Now, you need to instantiate it by passing a DOM selector, HTML element, or list of HTML elements."

new Clipboard('.btn');

so then I made an event listener like this:

 $('#test').click(function() {
            var clipboard = new Clipboard('#test');
        });

But I don't know what I'm supposed to do with the clipboard variable once it's created. Or have I missed the point on what I'm supposed to do entirely?

As you may surmise from this question, I'm not very good with javascript and am trying to get clipboard.js (https://clipboardjs./) working but can't. I followed instructions by copying clipboard.min.js into the scripts folder and then referenced it in my html file. Then I copied their button (and modified it a bit like this:

<button class="btn" id="test" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
    Copy to clipboard
</button>

In their setup instructions, they say this:

"Now, you need to instantiate it by passing a DOM selector, HTML element, or list of HTML elements."

new Clipboard('.btn');

so then I made an event listener like this:

 $('#test').click(function() {
            var clipboard = new Clipboard('#test');
        });

But I don't know what I'm supposed to do with the clipboard variable once it's created. Or have I missed the point on what I'm supposed to do entirely?

Share Improve this question edited Apr 27, 2016 at 13:30 aorcsik 15.6k5 gold badges42 silver badges50 bronze badges asked Apr 27, 2016 at 12:15 BMillsBMills 9012 gold badges10 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

I think the way you have to use it is simply instantiating after the DOM is loaded:

$(function() {
    new Clipboard('#test');
});

This will convert the button (with id="test") into a clipboard copy button. And pressing it will put the value of data-clipboard-text on the clipboard.

You don't even need to store this instance, unless you want to interact with the button later in the code.

new clipboardJS(“.btn”);

Check where you initialize the clipboard function. It should be new clipboardJS not new clipboard

Lets say #test is a textarea. We want to copy its value to clipboard the moment we click on it:

new ClipboardJS("#test", {
    text: function(trigger) {
        // do any other thing here
        return trigger.value.trim() ;
    }
});
发布评论

评论列表(0)

  1. 暂无评论