Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <title> 方案一:纯 JavaScript 复制内容到剪贴板</title>
  <link rel="canonical" href="https://yijile.com/log/307.html">
</head>
<body> 
  
<script type="text/javascript">
function copyToClip(content, message) {
    var aux = document.createElement("input"); 
    aux.setAttribute("value", content); 
    document.body.appendChild(aux); 
    aux.select();
    document.execCommand("copy"); 
    document.body.removeChild(aux);
    if (message == null) {
        alert("成功复制:" + content);
    } else{
        alert(message);
    }
}
</script>
  
<button onclick="copyToClip('内容2')"> Copy2 </button>
<button onclick="copyToClip('内容3')"> Copy3 </button>
<p style="margin: 5em 0 0">
  文章:<a href="https://yijile.com/log/307.html">纯 JS 复制内容到剪贴板</a>
</p>
  
</body>
</html>
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers