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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19606221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:46:00  来源:igfitidea点击:

Copy a text when a link or button is clicked

javascriptphphtml

提问by D P.

I am new to website development and try to figure out how can I make my user automatically copy a code in to his/her mouse(clip board) when clicked on a link (using html, php or javascript). For example, I am trying to create this personal website, when a user click on a link or a button in my website, it should automatically copy that text code to the clip board. I have seen sites like retailmenot.com do this: Example:- enter image description here

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

Please show me with an example if you can

如果可以,请举例说明



Updated:

更新:

<!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>

回答by Nikunj Dhimar

Here is the function which might can help you or future referer.

这是可能可以帮助您或未来推荐人的功能。

    function copyToClipboard(id) {
    var text = $("#td_id_" + id).text(); //getting the text from that particular Row
    //window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
    if (window.clipboardData && window.clipboardData.setData) {
        // IE specific code path to prevent textarea being shown while dialog is visible.
        return clipboardData.setData("Text", text); 

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  // Security exception may be thrown by some browsers.
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
  }

Unit test in all Browser not done.

未在所有浏览器中进行单元测试。

回答by Manwal

try this.

尝试这个。

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