JS 单击复制文本

target.select():选中target需为Input
document.execCommand("copy"):复制
window.getSelection().empty():清空选中状态
input需要设readonly=true,但不能设置disabled=true

  /*
    Dart.js
  */

  buildDetailTopItemForCopy("${conf.Lang.lang_cn["PRINCIPAL"]} : ",k2data?.contact, this._onCopyClick)
  
  Element buildDetailTopItemForCopy(
    String key, String value, ClickHandler clickHandler) {
    return Element.div()
      ..children.addAll([
        Element.span()..text = key,
        InputElement()
          ..value = (null == value ? "" : value)
          ..readOnly = true
          ..onClick.listen(clickHandler)
      ]);
  }

  void _onCopyClick(MouseEvent e) {
    print("_onCopyClick");
    InputElement target = e.target as InputElement;
    if (target.value == "") return;
    target.select();
    document.execCommand("copy");
    try {
      var copyResult = document.execCommand('copy');
      print("copy result::${copyResult}");
      if (copyResult)
        this._vm.popupModal("/toast", data: ToastIntent(""${target.value}"复制成功"));
    } catch (err) {
      print("copy err::${err}");
    }
    window.getSelection().empty();
  }
原文地址:https://www.cnblogs.com/yanjiez/p/14709169.html