使用“剪贴板”将文本复制到剪贴板。js“没有按钮

时间:2021-04-08 08:58:36

I use the https://clipboardjs.com/ library to copy text to the clipboard.

我使用https://clipboardjs.com/库将文本复制到剪贴板。

In all examples, you need to press the button. But how to implement it without a button?

在所有示例中,都需要按下按钮。但是如何在没有按钮的情况下实现它呢?

init_clipboard();
copy_to_clipboard ('mytext');

In clipboard.js example:

在剪贴板。js的例子:

var clipboard = new Clipboard('.btn', {
    text: function() {
        return 'to be or not to be';
    }
});

with button

与按钮

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

But I want without a button and user activity

但是我希望没有按钮和用户活动

1 个解决方案

#1


6  

Clipboard.js creator here. Many people have asked for this featured but this is a security limitation imposed by various browsers. If you try to simulate a click event using JavaScript it will not work as well. All this is because of preventing clipboard poisoning.

剪贴板。js的创造者。许多人都要求使用这个特性,但这是不同浏览器强加的安全限制。如果您试图使用JavaScript模拟单击事件,那么它将无法正常工作。所有这些都是因为防止剪贴板中毒。

#1


6  

Clipboard.js creator here. Many people have asked for this featured but this is a security limitation imposed by various browsers. If you try to simulate a click event using JavaScript it will not work as well. All this is because of preventing clipboard poisoning.

剪贴板。js的创造者。许多人都要求使用这个特性,但这是不同浏览器强加的安全限制。如果您试图使用JavaScript模拟单击事件,那么它将无法正常工作。所有这些都是因为防止剪贴板中毒。