admin管理员组文章数量:1290949
I want to add a "copy to clipboard" button on my website. The site uses the Bootstrap 3 framework. I want my button to work similarly to the "Copy to Clipboard" button used here: /
I tried to incorporate this code: /, but I have had no success with it.
Javascript:
$("a.copy").on('click', function (e) {
e.preventDefault();
}).each(function () {
$(this).zclip({
path: '.swf',
copy: function() {
return $(this).data('copy');
}
});
});
CSS:
body {
padding: 20px
}
HTML
<hr />
<h5>These copy to clipboard links are working...</h5>
<p><a href="#" data-copy="/" class="copy">Copy Original Link</a></p>
<p><a href="#" data-copy="/" class="copy">Copy Medium Link</a></p>
<p><a href="#" data-copy="/" class="copy">Copy Web Link</a></p>
<hr />
<h5>If I put these links inside the bootstrap dropdown, they stop working...</h5>
<div class="btn-group">
<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">
View copy clipboard links
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#" data-copy="/" class="copy">Copy Original Link</a></li>
<li><a href="#" data-copy="/" class="copy">Copy Medium Link</a></li>
<li><a href="#" data-copy="/" class="copy">Copy Web Link</a></li>
<li class="divider"></li>
<li><a href="mailto:" title="Email URL Links">Email URL Link</a></li>
</ul>
</div>
Any ideas on how I should add this to a button on my Bootstrap 3 site? Or are there any other good alternatives?
Thanks! :)
I want to add a "copy to clipboard" button on my website. The site uses the Bootstrap 3 framework. I want my button to work similarly to the "Copy to Clipboard" button used here: http://twitterbootstrapbuttons.w3masters.nl/
I tried to incorporate this code: http://jsfiddle/T2uXr/, but I have had no success with it.
Javascript:
$("a.copy").on('click', function (e) {
e.preventDefault();
}).each(function () {
$(this).zclip({
path: 'http://www.steamdev./zclip/js/ZeroClipboard.swf',
copy: function() {
return $(this).data('copy');
}
});
});
CSS:
body {
padding: 20px
}
HTML
<hr />
<h5>These copy to clipboard links are working...</h5>
<p><a href="#" data-copy="http://test.one./" class="copy">Copy Original Link</a></p>
<p><a href="#" data-copy="http://test.two./" class="copy">Copy Medium Link</a></p>
<p><a href="#" data-copy="http://test.three./" class="copy">Copy Web Link</a></p>
<hr />
<h5>If I put these links inside the bootstrap dropdown, they stop working...</h5>
<div class="btn-group">
<a class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">
View copy clipboard links
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#" data-copy="http://test.one./" class="copy">Copy Original Link</a></li>
<li><a href="#" data-copy="http://test.two./" class="copy">Copy Medium Link</a></li>
<li><a href="#" data-copy="http://test.three./" class="copy">Copy Web Link</a></li>
<li class="divider"></li>
<li><a href="mailto:" title="Email URL Links">Email URL Link</a></li>
</ul>
</div>
Any ideas on how I should add this to a button on my Bootstrap 3 site? Or are there any other good alternatives?
Thanks! :)
Share Improve this question edited Jul 2, 2015 at 8:47 Siguza 23.9k6 gold badges55 silver badges98 bronze badges asked Apr 6, 2014 at 21:00 user3504529user3504529 711 gold badge1 silver badge2 bronze badges 2-
There is no magic to it, you just include the script on your page, and every link with class
copy
will then be a clipboard copy link. Of course you also have to include thezclip
library and you can change the selector to choose any other element(s). – Felix Kling Commented Apr 6, 2014 at 21:17 - use code snippets – Adil Saju Commented Nov 16, 2018 at 6:35
2 Answers
Reset to default 2Here is how I have fixed the issue while working on it till late night 4:00 AM and posting my issue (click here to see), hope this will help some one to not burn his night :) I tried to do how Bootstrap current website does for copying it's code, but Bootstrap is currently using old zeroClipboard Plugin. I have tried to make same as Bootstrap did but with latest zeroClipboard (as of now).
HTML: Note how I have not added all the code in one line, do not bring pre
& code
in second line or indent it to make code look good else the extra intend will copied to clip board.
<div class="highlight"><pre><code>Some code/text goes here</code></pre></div>
CSS:
/* ZeroClipboard styles */
.zero-clipboard {
position: relative;
display: none;
}
.btn-clipboard {
position: absolute;
top: 0;
right: 0;
z-index: 10;
display: block;
padding: 5px 8px;
font-size: 12px;
color: #777;
cursor: pointer;
background-color: #fff;
border: 1px solid #e1e1e8;
border-radius: 0 4px 0 4px;
}
.btn-clipboard-hover {
color: #fff;
background-color: #563d7c;
border-color: #563d7c;
}
@media (min-width: 768px) {
.zero-clipboard {
display: block;
}
.zero-clipboard .btn-clipboard {
top: -16px;
border-top-right-radius: 0;
}
}
JavaScript: place ZeroClipboard.swf
where the JS file is.
// Config ZeroClipboard
ZeroClipboard.config({
hoverClass: 'btn-clipboard-hover'
})
// Insert copy to clipboard button before .highlight
$('.highlight').each(function () {
var btnHtml = '<div class="zero-clipboard"><span class="btn-clipboard">Copy</span></div>';
$(this).before(btnHtml)
});
var zeroClipboard = new ZeroClipboard($('.btn-clipboard'));
var htmlBridge = $('#global-zeroclipboard-html-bridge');
// Handlers for ZeroClipboard
zeroClipboard.on('ready', function (event) {
htmlBridge
.data('placement', 'top')
.attr('title', 'Copy to clipboard')
.tooltip();
// Copy to clipboard
zeroClipboard.on('copy', function (event) {
var highlight = $(event.target).parent().nextAll('.highlight').first();
event.clipboardData.setData("text/plain", highlight.text())
});
// Notify copy success and reset tooltip title
zeroClipboard.on('aftercopy', function () {
htmlBridge
.attr('title', 'Copied!')
.tooltip('fixTitle')
.tooltip('show')
.attr('title', 'Copy to clipboard')
.tooltip('fixTitle')
});
});
// Notify copy failure
zeroClipboard.on('error', function () {
ZeroClipboard.destroy();
htmlBridge
.attr('title', 'Flash required')
.tooltip('fixTitle')
.tooltip('show');
});
Your code looks fine and also your jsfiddle code works correctly. I tried it in Safari, Chrome, and Firefox on Mac OS X and they all worked. As your code requires Flash, make sure to have Flash installed and enabled.
本文标签: javascriptCopy to clipboard in bootstrap 3Stack Overflow
版权声明:本文标题:javascript - Copy to clipboard in bootstrap 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741500412a2382028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论