简体   繁体   English

document.execCommand('copy')在Firefox中不适用于Codemirror textarea字段

[英]document.execCommand('copy') doesn't work in firefox for codemirror textarea field

I am using a custom confirmation dialogue with a textarea(codemirror) on it with some text populated. 我正在使用带有文本区域(代码镜像)的自定义确认对话框,其中填充了一些文本。 As code mirrors hides the actual textarea element, firefox is not able to get the data from the hidden textarea field. 由于代码镜像隐藏了实际的textarea元素,因此Firefox无法从隐藏的textarea字段获取数据。 The definition for the confirmation box goes like below: 确认框的定义如下:

var confirmationDialog = MD.ui.dialogs.confirm({
    title: title,
    text: '',
    type: 'dataUri',
    dataUri: formUrl,
    position: 'center',
    buttonForward: {
        text: 'Copy',
        action: function () {
            DataGridExportDialog.CopyToClipboard("#rawXmlImpExp");
        }
    },
    buttonCancel: {
        text: 'Cancel',
        action: function () {
            confirmationDialog.close();
            confirmationDialog.destroy();
        }
    }
});

According to the requirement I have updated the confirm button functionality with copy to clipboard functionality so that on clicking on 'Copy' the text in the textarea should be copied to clipboard. 根据要求,我使用复制到剪贴板功能更新了确认按钮功能,以便在单击“复制”时将文本区域中的文本复制到剪贴板。 Below is the copyToClipboard(). 下面是copyToClipboard()。

DataGridExportDialog.CopyToClipboard = function( containerId ) {
/*var textareaData = $('#rawXmlImpExp').val();
var range = document.createRange();
range.selectNodeContents(textareaData);
window.getSelection().addRange(range);*/

var copyTextarea = document.querySelector(containerId);
copyTextarea.select();

try {
    var successful = document.execCommand('copy');
    raiseMessage('Configuration XML copied to clip board.')
} catch (err) {
    raiseWarning('Unable to copy. Please do so manually.');
}}

This implementation works fine on chrome but fails in firefox. 此实现在chrome上运行良好,但在Firefox中失败。 Any ideas where my code fails on firefox. 我的代码在firefox上失败的任何想法。

Unhided the textarea in try block and hidden the same in finally block. 在try块中取消隐藏文本区域,在finally块中隐藏相同的文本区域。

CopyToClipboard = function( containerId ) {
  const copyTextarea = $(containerId);
  try {
    $(copyTextarea).css('display','block');
    copyTextarea[0].select();
    document.execCommand('copy');
    raiseMessage('Configuration XML copied to clip board.');
  } 
  catch (err) {
    raiseWarning('Unable to copy. Please do so manually.');
  }
  finally {
    $(copyTextarea).css('display','none');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM