微信小程序web-view网页点击复制到剪贴板

本文由清尘发表于2019-04-02 12:59最后修改于2019-07-09属于移动前端分类

HTML:

<input type="text" value="test1111" id="wx_txn2" class="wx_input g_put">
<span  onclick="copytext('wx_txn2','test1111')" class="g_fz">点击复制</span>

JS:

function copytexta(h,hm) {
    var d,a = document.getElementById(h);
    var c = a.hasAttribute("readonly");
    if (!c) {
        a.setAttribute("readonly", "")
    }
    a.select();
    a.setSelectionRange(0, a.value.length);
    if (!c) {
        a.removeAttribute("readonly")
    }
    d = a.value;
    try {
        document.execCommand("copy", false, null)
    } catch(b) {
        alert("您的系统不支持,请长按后复制")
    }
}