齐枫的博客

just a noob!

js复制操作

vue下实现复制操作

<template>
    <div class="red">
        <div @click="copyToClipboard($event)">
            {{msg+222}}
        </div>
        <input type="text" ref="clickboard" v-model="msg">
        {{msg}}
    </div>
</template>
<script>
export default {
    data(){
        return {
            msg:111111
        }
    },
    methods:{
        copyToClipboard(e){
            // console.log(e)
            console.log(e.target.value)
            console.log(this.$refs.clickboard)
            this.$refs.clickboard.select()
            document.execCommand('copy')
        }
    }
}
</script>
<style scoped>
    .red{
        width: 100px;
        height: 50px;
        background:red;
    }
    .red input{
        position: absolute;
        top:-9999px;
        opacity: 0;
        transform:scale(0);
    }
</style>

复制操作2


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div{
            width:100%;
            height: 50px;
            background:aqua;
        }
    </style>
</head>
<body>
    <div id="dd">222</div>
    <script>
        function Copy(str){
        var save = function(e){
            e.clipboardData.setData('text/plain', str);
            e.preventDefault();
        }
        document.addEventListener('copy', save);
        document.execCommand('copy');
        document.removeEventListener('copy',save);
            alert('复制成功!');
        }
        // $('#cardList').on('click', 'div.btn', function(){
        //     Copy($(this).prev('div').find('span.code').text());
        // });    
        var dd = document.getElementById('dd')
        dd.onclick = function(){
            alert(2)
            Copy(dd.innerText)
        }
    </script>
</body>
</html>

iphone下的复制操作支持

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
    <style>
        div{
            width:800px;
            height: 500px;
            background:red;
            cursor: pointer;
        }
    </style>
<body>
    <div id="ee" onclick="aa()">12312312312</div>
  <script>

    function aa(){
        var copyDOM = document.getElementById('ee');
        // console.log(copyDOM)
        var range = document.createRange();
        // 选中需要复制的节点
        range.selectNode(copyDOM);
        // 执行选中元素
        window.getSelection().addRange(range);
        // 执行 copy 操作
        var successful = document.execCommand('copy');
        // alert(successful);
        try {
        var msg = successful ? 'successful' : 'unsuccessful';
        alert('copy is' + msg);
        } catch (err) {
        alert('Oops, unable to copy');
        }
        // 移除选中的元素
        window.getSelection().removeAllRanges();
    }
  </script>
</body>

</html>

vue使用复制功能推荐vue-clipboard2

阅读更多
版权声明:多总结,帮助自己的同时也在帮助别人!留下意见和看法,让技术动起来! https://blog.csdn.net/qiphon3650/article/details/79977120
文章标签: js实现复制操作
个人分类: javascript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

js复制操作

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭