【问题标题】:How to execute ctrl+c or copy commad using javascript or jquery on button click如何在单击按钮时使用 javascript 或 jquery 执行 ctrl+c 或复制命令
【发布时间】:2014-03-20 10:55:01
【问题描述】:

是否可以使用click EVENT执行复制命令?

我选择了一些文本,我希望在onClick 事件中复制这些文本,这样我就可以在不使用右键单击或 CTRL+C 复制文本。

【问题讨论】:

    标签: javascript jquery onclick copy


    【解决方案1】:
    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");
    }
    

    但出于安全原因,大多数浏览器都不允许修改剪贴板( Internet explorer除外)

    【讨论】:

    • 我认为不是 "document.getElementById("a").value="txt";"你的意思是“document.getElementById("a").value=txt;”但除此之外,这对我帮助很大。谢谢
    【解决方案2】:

    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;
    }
    

    复制到剪贴板 Ctrl+C

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

    【讨论】:

      【解决方案3】:

      使用getselection() 在浏览器窗口中获取选定的文本

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-23
        • 2018-08-11
        • 1970-01-01
        相关资源
        最近更新 更多