一款给力的一键复制js插件-clipboard.js

释放双眼,带上耳机,听听看~!

一款没有依赖的、给力的一键复制的JS插件  点我前往github

案例demo见下载包内demo文件夹。

这里晒出最常用的几种方式,以供不时之需。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>function-target</title><meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><!-- 1. Define some markup --><button class="btn">Copy</button><div>hello</div>

    <!-- 2. Include library --><script src="../dist/clipboard.min.js"></script>
<!-- 3. Instantiate clipboard --><script>var clipboard = new ClipboardJS('.btn', {
        target: function() {
            return document.querySelector('div');
        }
    });

    clipboard.on('success', function(e) {
        //console.log(e);
        alert('复制成功!')
    });

    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>function-text</title><meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><!-- 1. Define some markup --><button class="btn">Copy</button>

    <!-- 2. Include library --><script src="../dist/clipboard.min.js"></script>

    <!-- 3. Instantiate clipboard --><script>var clipboard = new ClipboardJS('.btn', {
        text: function() {
            return 'to be or not to be';
        }
    });

    clipboard.on('success', function(e) {
        console.log(e);
    });

    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>target-div</title><meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><!-- 1. Define some markup --><span class="copy_content">hello 123</span><button class="btn" data-clipboard-action="copy" data-clipboard-target=".copy_content">Copy</button>

    <!-- 2. Include library --><script src="../dist/clipboard.min.js"></script>

    <!-- 3. Instantiate clipboard --><script>var clipboard = new ClipboardJS('.btn');

    clipboard.on('success', function(e) {
        console.log(e);
    });

    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>target-input</title><meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><!-- 1. Define some markup --><input id="foo" type="text" value="hello"><button class="btn" data-clipboard-action="copy" data-clipboard-target="#foo">Copy</button>

    <!-- 2. Include library --><script src="../dist/clipboard.min.js"></script>

    <!-- 3. Instantiate clipboard --><script>var clipboard = new ClipboardJS('.btn');

    clipboard.on('success', function(e) {
        console.log(e);
    });

    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>target-textarea</title><meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body><!-- 1. Define some markup --><textarea id="bar">hello</textarea><button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">Cut</button>

    <!-- 2. Include library --><script src="../dist/clipboard.min.js"></script>

    <!-- 3. Instantiate clipboard --><script>var clipboard = new ClipboardJS('.btn');

    clipboard.on('success', function(e) {
        console.log(e);
    });

    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>
</body>
</html>

人已赞赏
编程

阿里云/腾讯云服务器后台监控一键卸载脚本

2019-3-5 8:56:49

编程

一个网页端表格JS库x-spreadsheet

2019-3-11 21:54:44

个人中心
今日签到
有新私信 私信列表
搜索