html 一键复制


 //一键复制
        $("#b_identifier").click(function() {
            var ssrsss = $(".label-content").attr('title');//获取文本
            var flag = copyText(ssrsss); //传递文本
            flag ? layer.msg('复制成功!', {icon: 1}) : layer.msg('复制失败!', {icon: 2});
        })
        function copyText(text) {
            var textarea = document.createElement("input");//创建input对象
            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;
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM