Clipboard.js无法在Bootstrap模式下工作

时间:2016-11-10 11:11:14

标签: javascript jquery twitter-bootstrap clipboard.js

我正在尝试使用Clipboard.js复制输入值:https://clipboardjs.com/。输入位于模态:

http://codepen.io/Deka87/pen/eBJOKY

new Clipboard('#copy', {
    text: function(trigger) {
        return $("#copy-input").val();
    }
});

虽然它在模态之外工作,但当输入和复制按钮位于模态窗口中时,它无法工作。我试图在模态窗口打开后启动剪贴板功能:

$(".modal").on("shown.bs.modal", function() {
  new Clipboard('#copy', {
      text: function(trigger) {
          return $("#copy-input").val();
      }
  });
});

然而,它并没有解决问题。有什么想法吗?

6 个答案:

答案 0 :(得分:4)

试试这个分叉:http://codepen.io/anon/pen/NbxWbQ 我忘了删除console.log所以请忽略它:)

<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>

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

答案 1 :(得分:3)

您必须设置容器

new ClipboardJS('.btn', {
    container: document.getElementById('modal')
});

请参阅“高级用法”部分的https://clipboardjs.com/页面。

答案 2 :(得分:1)

我遇到了同样的问题,我解决了这个问题,而不是document.body将这个元素添加到模态中。

function copyToClipboard() {
   const el = document.createElement('textarea');
    el.value = 'text to copy';
    document.getElementById('copy').appendChild(el);
    el.select();
    document.execCommand('Copy');
    document.getElementById('copy').removeChild(el);
}

Bootstrap的模式强制关注可访问性(enforceFocus)的原因但会导致许多第三方库出现问题

https://github.com/twbs/bootstrap/issues?q=is:issue+enforceFocus+is:closed

答案 3 :(得分:0)

我已经尝试了所有可能的案例,但没有一个能够奏效。因此,我没有使用剪贴板,只是做了一些js技巧。

  1. 首先选择要复制的文本。

    document.querySelector('#txtCopy').select();

  2. 但只有当您的元素是文本框时,此代码才有效。那么如何选择是否要选择div或span等内容。那么你可以使用以下函数来做到这一点 -

    function selectText(element) {
      if (/INPUT|TEXTAREA/i.test(element.tagName)) {
        element.focus();
        if (element.setSelectionRange) {
          element.setSelectionRange(0, element.value.length);
        } else {
          element.select();
        }
        return;
      }
    
      if (window.getSelection) { // All browsers, except IE <=8
        window.getSelection().selectAllChildren(element);
      } else if (document.body.createTextRange) { // IE <=8
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
      }
    }
    
    1. 现在我们需要复制选定的文字 -

      document.execCommand('copy');

    2. 现在您可以看到文本被复制了。

      有时您需要在复制后取消选择所有文本。在这种情况下 - 您可以使用以下功能来取消选择 -

      function deselectAll() {
        var element = document.activeElement;
      
        if (element && /INPUT|TEXTAREA/i.test(element.tagName)) {
          if ('selectionStart' in element) {
            element.selectionEnd = element.selectionStart;
          }
          element.blur();
        }
      
        if (window.getSelection) { // All browsers, except IE <=8
          window.getSelection().removeAllRanges();
        } else if (document.selection) { // IE <=8
          document.selection.empty();
        }
      }
      

      希望这适合你。

答案 4 :(得分:0)

https://github.com/zenorocha/clipboard.js/issues/155#issuecomment-217372642

Bootstrap 3

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

引导程序4

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

答案 5 :(得分:0)

我遇到了类似的问题,并通过以下步骤获得了解决方案: 1)创建一个临时输入元素并将值添加到其中: var $temp_input = $("<input value='" + valueToCopy + "'>"); 2)将元素追加到您的模式弹出窗口 $("#ModalPopup").append($temp_input); 3)设置焦点并选择字段:  $temp_input.focus(); $temp_input.select(); 4)使用document.execCommand("copy") 5)删除临时元素  $temp_input.remove();