javaScript如何复制粘贴指定标签中的内容

2025年02月28日 建站教程

javaScript如何复制粘贴指定div中的内容,下面web建站小编带大家了解一下如何通过document.execCommand('copy')来实现这个功能!

先引入sweetalert.min.css和sweetalert.min.js

复制粘贴实现代码如下:

function copyText(str_file) {
 const btn = document.querySelector('.'+str_file);
 var copy_val = document.getElementById(str_file)
 var copy_file = copy_val.getAttribute("href");
 btn.addEventListener('click',() => {
  const input = document.createElement('input');
  document.body.appendChild(input);
  input.setAttribute('value', copy_file);
  input.select();
  if (document.execCommand('copy')) {
   document.execCommand('copy');
   swal("复制成功!","success");
  }
  document.body.removeChild(input);
 })
}

本文链接:http://so.lmcjl.com/news/23998/

展开阅读全文
相关内容