[英]How do I use Clipboard.js dynamically with background color?
I have a button: 我有一个按钮:
<button class="badge" style="background-color: #ff3333;" data-clipboard-target="#badge">RED</button>
that I'd like to copy the color of using Clipboard.js. 我想复制使用Clipboard.js的颜色。 I understand that I can do this manually with data-clipboard-target but I was hoping to trigger the event dynamically using something like their sample code : 我了解我可以使用data-clipboard-target手动执行此操作,但我希望使用其示例代码来动态触发事件:
new ClipboardJS('.btn', {
text: function(trigger) {
return trigger.getAttribute('aria-label');
}};
I tried using about 100 variants of: 我尝试使用约100种变体:
new ClipboardJS(".badge", {
text: function(trigger) {
return $(trigger).closest(".badge").element.style.backgroundColor();
}
});
but I keep getting the error: Uncaught TypeError: Cannot read property 'style' of undefined
. 但我不断收到错误: Uncaught TypeError: Cannot read property 'style' of undefined
。
I realize that I can just use data-clipboard-target and do this manually but I was hoping figure out why the targeting is off. 我意识到我可以只使用data-clipboard-target并手动执行此操作,但我希望弄清楚为什么关闭了目标。 Thanks 谢谢
You have to set the data-clipboard-text
and then return the text you want copied in the text function
. 您必须设置data-clipboard-text
,然后在text function
返回要复制的text function
。 Also you can use trigger.style.backgroundColor
to get the background color. 您也可以使用trigger.style.backgroundColor
来获取背景色。
new ClipboardJS(".badge", { text: function(trigger) { var result = trigger.style.backgroundColor return console.log(result) || result } });
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="badge" style="background-color: #ff3333;" data-clipboard-text="">RED</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.