231009 142747.gif

document.execCommand('copy')方法复制

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
<div id="linksContainer">
  <!-- 初始加载的 <a> 标签 -->
  <a data-ww="内容1">链接1</a>
  <a data-ww="内容2">链接2</a>
  <a data-ww="内容3">链接3</a>
</div>

<script>
  // 获取容器元素
  var linksContainer = document.getElementById('linksContainer');

  // 添加点击事件委托给容器元素
  linksContainer.addEventListener('click', function(event) {
    // 检查被点击的元素是否为 <a> 标签
    if (event.target.tagName === 'A') {
      copyData(event.target);
    }
  });

  function copyData(elem) {
    var dataWw = elem.getAttribute('data-ww');
    var dummyInput = document.createElement('input');
    document.body.appendChild(dummyInput);
    dummyInput.value = dataWw;
    dummyInput.select();
    document.execCommand('copy');
    document.body.removeChild(dummyInput);
  }
</script>

</body>
</html>

navigator.clipboard.writeText() 方法复制

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
    
<div id="linksContainer">
  <!-- 初始加载的 <a> 标签 -->
  <a data-ww="内容1">链接1</a>
  <a data-ww="内容2">链接2</a>
  <a data-ww="内容3">链接3</a>
</div>

<script>
  // 获取容器元素
  var linksContainer = document.getElementById('linksContainer');

  // 添加点击事件委托给容器元素
  linksContainer.addEventListener('click', function(event) {
    // 检查被点击的元素是否为 <a> 标签
    if (event.target.tagName === 'A') {
      copyData(event.target);
    }
  });

  function copyData(elem) {
    var dataWw = elem.getAttribute('data-ww');
    navigator.clipboard.writeText(dataWw).then(function() {
      alert('复制成功!');
    }).catch(function() {
      alert('复制失败!请手动复制内容。');
    });
  }
</script>

</body>
</html>