admin管理员组文章数量:1435859
I need to copy the content of a <code></code>
section using ClipboardJS
. When following the tutorial, I end up with this error :
Uncaught Error: Invalid "target" value, use a valid Element
Any workaround on this ?
EDIT:
HTML
<code id="#foo">my fantastic code</code>
<button class="copy-button" data-clipboard-action="copy" data-clipboard-target="#foo">
Copy !
</button>
JAVASCRIPT
<script type="text/javascript">
var clipboard = new Clipboard('.copy-button');
clipboard.on('success', function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
e.clearSelection();
});
clipboard.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
</script>
I need to copy the content of a <code></code>
section using ClipboardJS
. When following the tutorial, I end up with this error :
Uncaught Error: Invalid "target" value, use a valid Element
Any workaround on this ?
EDIT:
HTML
<code id="#foo">my fantastic code</code>
<button class="copy-button" data-clipboard-action="copy" data-clipboard-target="#foo">
Copy !
</button>
JAVASCRIPT
<script type="text/javascript">
var clipboard = new Clipboard('.copy-button');
clipboard.on('success', function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
e.clearSelection();
});
clipboard.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
</script>
Share
Improve this question
edited Oct 12, 2021 at 1:20
Rifky Niyas
2,0761 gold badge14 silver badges29 bronze badges
asked Feb 23, 2017 at 17:52
user4187476user4187476
3
-
Where is your actual code? It looks like you aren't selecting the element correctly. You can see here that it works fine on
code
elements. – Mike Cluck Commented Feb 23, 2017 at 17:53 - @MikeC Thank you for your input. I edited with my code – user4187476 Commented Feb 23, 2017 at 18:00
-
3
You have a typo. It should be
<code id="foo">...
, not<code id="#foo">...
– Mike Cluck Commented Feb 23, 2017 at 18:01
1 Answer
Reset to default 7Try to change this line:
<code id="#foo">my fantastic code</code>
for
<code id="foo">my fantastic code</code>
本文标签: javascriptClipboardJSCopy the content of a ltcodegt sectionStack Overflow
版权声明:本文标题:javascript - ClipboardJS : Copy the content of a <code> section - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745668346a2669417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论