How can I copy to clipboard in HTML5 without using flash?

前端 未结 6 1323
一个人的身影
一个人的身影 2020-12-01 07:39

I want to use a copy-to-clipboard function in HTML5, but without using flash. Is it possible? How?

I tried to implement a copy-to-clipboad function with JavaScript,

6条回答
  •  广开言路
    2020-12-01 07:59

    If you don't care that the contents of the text field will be selected prior to copy, here is two-line solution that works at least in Chrome 56 and Edge, but I bet it works in other browsers as well.

    function clickListener() {
      document.getElementById('password').select();
      document.execCommand('copy');
    }
    
    document.getElementById('copy_btn').addEventListener('click', clickListener);
    
    

    https://jsfiddle.net/uwd0rm08/

提交回复
热议问题