繁体   English   中英

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

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

我正在使用带有文本区域(代码镜像)的自定义确认对话框,其中填充了一些文本。 由于代码镜像隐藏了实际的textarea元素,因此Firefox无法从隐藏的textarea字段获取数据。 确认框的定义如下:

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();
        }
    }
});

根据要求,我使用复制到剪贴板功能更新了确认按钮功能,以便在单击“复制”时将文本区域中的文本复制到剪贴板。 下面是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.');
}}

此实现在chrome上运行良好,但在Firefox中失败。 我的代码在firefox上失败的任何想法。

在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