jquery 复制文字到剪贴板

复制到剪贴板

<html>
<button class="copy" type="submit">
    复制链接
</button>
<script>
    function copyText(text) {
        var textarea = document.createElement("textarea");
        var currentFocus = document.activeElement;
        document.body.appendChild(textarea);
        textarea.value = text;
        textarea.focus();
        if (textarea.setSelectionRange)
            textarea.setSelectionRange(0, textarea.value.length);
        else
            textarea.select();
        try {
            var flag = document.execCommand("copy");
        } catch(eo){
            var flag = false;
        }
        document.body.removeChild(textarea);
        currentFocus.focus();
        return flag;
    }
    $(".copy").on('click',function(){
        var text='复制的文字';
        var flag = copyText(text);
        alert(flag ? "复制成功!" : "复制失败!");
    });
</script>
</html>
posted @ 2020-10-30 19:25  邈宇  阅读(896)  评论(0)    收藏  举报