欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js拷贝指定内容到剪切板

程序员文章站 2022-06-16 10:33:45
...
 function copyTextToClipboard(text) {  
  var textArea = document.createElement("textarea");   
  textArea.style.background = 'transparent';  
  textArea.value = text;  
  document.body.appendChild(textArea);  
  textArea.select();  
  try {    
    var successful = document.execCommand('copy');    
    var msg = successful ? 'successful' : 'unsuccessful';          
  } catch (err) {  
  console.log('Oops, unable to copy');  
  }  
 document.body.removeChild(textArea);}