点击按钮将内容赋值到粘贴板

以下代码实现了点击将内容复制到粘贴板随处粘贴的效果

很实用!!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>function-text</title>
</head>
<body>
<button class="btn">复制</button>
<div id="c">SUCCEED</div>

//引用clipboard.min.js实现将内容复制
<!-- <script src="http://www.jq22.com/demo/copy20161120/dist/clipboard.min.js"></script> -->
<script src="./clipboard.min.js"></script>

<script>
var c = document.getElementById("c");
var s = c.innerHTML;
var clipboard = new Clipboard('.btn', {
text: function() {
return s;
}
});

 

//检验是否复制成功
clipboard.on('success', function(e) {
console.log("复制成功")
});

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

</script>
</body>
</html>

posted @ 2017-12-29 18:35  kimsaerono  阅读(364)  评论(0编辑  收藏  举报