1. 程式人生 > >在bootstrap模態框modal裡使用clipboard.js時複製失效

在bootstrap模態框modal裡使用clipboard.js時複製失效

中文居然沒搜尋到什麼結果,遂google之。

原因:

沒有獲得焦點文件↓)

For use in Bootstrap Modals or with any other library that changes the focus you'll want to set the focused element as the container value.

解決方法:

1.增加焦點容器(我用的):

html:

<input type="text" class="form-control" id="copy-input" value="Copied successfully!"/>
    <br />
<a href="#" id="copy" data-clipboard-target="#copy-input" class="btn btn-default">Copy input content to clipboard</a>

JS:

new Clipboard('.btn', {
    container: document.getElementById('modal') //html所在模態框ID
});

P.S.很奇怪,用data-clipboard-text 失效,只有這種html裡分開的才成功

2.模態框的事件裡增加

$(".modal").on("shown.bs.modal",function(){
  console.log('a',Clipboard, $('#copy'), $("#copy-input").val());var clipboard =newClipboard('#copy'
)});

3.其它(未嘗試):

增加一句:

$.fn.modal.Constructor.prototype.enforceFocus = function() {};

參考:

http://www.cnblogs.com/yunser/p/7628031.html

https://stackoverflow.com/questions/40490733/using-clipboard-js-in-a-bootsrap-modal

https://stackoverflow.com/questions/40526156/clipboard-js-not-working-in-bootstrap-modal

https://stackoverflow.com/questions/38398070/bootstrap-modal-does-not-work-with-clipboard-js-on-firefox

https://github.com/zenorocha/clipboard.js/issues/155