单击链接或按钮时复制文本

D P*_* P. 10 html javascript php

我是网站开发的新手,并试图弄清楚如何在点击链接(使用html,php或javascript)时让我的用户自动将代码复制到他/她的鼠标(剪贴板)中.例如,我正在尝试创建这个个人网站,当用户点击我网站上的链接或按钮时,它应该自动将该文本代码复制到剪贴板.我看过像retailmenot.com这样的网站这样做:示例: - 在此输入图像描述

如果可以,请告诉我一个例子


更新:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>


$("#link").click(function(){
  var holdtext = $("#clipboard").innerText;
  Copied = holdtext.createTextRange();
  Copied.execCommand("Copy");
});


</script>
</head>
<body>

<hr>
<a href="http://www.w3schools.com" style="font-family:arial;color:black;font-size:25px;">Click here to copy the Code</a> <button onclick="copyToClipboard()">Copy Text</button>
<hr>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Man*_*wal 1

尝试这个。

$("#link").click(function(){
  var holdtext = $("#clipboard").innerText;
  Copied = holdtext.createTextRange();
  Copied.execCommand("Copy");
});
Run Code Online (Sandbox Code Playgroud)