【问题标题】:Separate copy buttons to copy separate input fields without redundancies单独的复制按钮以复制单独的输入字段而没有冗余
【发布时间】:2020-05-15 08:23:17
【问题描述】:

我有这个(我知道这是错误的):

function copy() {
    var copyText = document.getElementById("copy_");
    copyText.select();
    copyText.setSelectionRange(0, 99999)
    document.execCommand("copy");
}
function copy2() {
    var copyText2 = document.getElementById("copy_2");
    copyText2.select();
    copyText2.setSelectionRange(0, 99999)
    document.execCommand("copy2");
}

它链接到这些:

<ol>
<li><p>Open Terminal and change directory to /test/:</p><input type="text" value="cd /Users/test/" id="copy_" disabled><button onclick="copy()">Copy</button></li>

<li><p>Make a directory:</p><input type="text" value="mkdir .test" id="copy_2" disabled><button onclick="copy2()">Copy</button></li>
</ol>

有人告诉我的一些同事使用事件对象和querySelectorAll,但我不确定如何合并它。

提前致谢

【问题讨论】:

    标签: javascript function input copy


    【解决方案1】:

    我不确定在这种情况下是否使用事件对象和 querySelectorAll。

    但是你有 2 个相似的方法在元素的“id”上有所不同。

    创建一个将元素的“id”作为函数参数的方法

    jsfiddle

    function copy(id) {
        var copyText = document.getElementById(id);
        copyText.select();
        copyText.setSelectionRange(0, 99999)
        document.execCommand("copy");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多