How to execute ctrl+c or copy commad using javascr

2019-02-23 11:31发布

Is it possible to execute copy command using click EVENT?

I have some text selected and I want this text to be copied on onClick event, so that I am able to past this text to another page with out using right click or CTRL+C to copy the text.

3条回答
相关推荐>>
2楼-- · 2019-02-23 11:36

HTML

<form name="myForm">
<span onclick="copyText(this)" >Text1</span>, <span onclick="copyText(this)" >Text2</span>
<br>
<input name="myField"></input>

JavaScript

function copyText(element) {
document.myForm.myField.value = element.innerHTML;
}

Copy to Clip Board Ctrl+C

$("#text1").click(function(){
var holdtext = $("#clipboard").innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
});
查看更多
太酷不给撩
3楼-- · 2019-02-23 11:40

use getselection() to get selected text inside a browser window

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-23 11:46
function copyText(){
    var txt = '';
     if (window.getSelection)
        txt = window.getSelection();
    else if (document.getSelection)
        txt = document.getSelection();
    else return;
    document.getElementById("a").value=txt;
    allCopied =document.getElementById("a").createTextRange();
    allCopied.execCommand("RemoveFormat");

   allCopied.execCommand("Copy");
}

but for security reasons most browsers do not allow to modify the clipboard( except Internet explorer).

查看更多
登录 后发表回答