如何将文本复制到剪贴板?

时间:2019-12-17 12:23:59

标签: javascript copy clipboard

如果用户未定义电子邮件客户端,则需要将电子邮件地址复制到剪贴板。 我在网上某处找到了解决方案:

html:

<input type="text" value="someemail@gmail.com" id="myInput" style="display:none";>

js:

  function copyEmail() {

  var copyText = document.getElementById("myInput");

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999);

  /* Copy the text inside the text field */
  document.execCommand('copy');

  /* Alert the copied text */
  alert("You don't have an email client defined"  + '\n' + "Email address copied: " + copyText.value + ".");
}

  $('a[href^=mailto]').each(function() {
    var href = $(this).attr('href');
    $(this).click(function() {
      var t;
      var self = $(this);

      $(window).blur(function() {
        // The browser apparently responded, so stop the timeout.
        clearTimeout(t);
      });

      t = setTimeout(function() { copyEmail() }, 1);

    });
  });

它获取值,但不会将其复制到剪贴板。我在做什么错了?

0 个答案:

没有答案