js实现粘贴到剪贴板的功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>copy example</title>
</head>
<body>
<input type="text" placeholder="请输入文字" id="selector">
<button id="copy">点击复制</button>
</body>
<script>
var copy = document.querySelector('#copy')
copy.addEventListener('click',()=>{
var val = document.querySelector("#selector");
val.select()
document.execCommand('Copy')
},false)
</script>
</html>
第二个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>copy example</title>
</head>
<body>
<a id="selector">我是老大</a>
<button id="copy">点击复制</button>
</body>
<script>
var aEle = document.querySelector('#copy');
aEle.addEventListener('click',function(){
var copyDOM = document.querySelector('#selector');
var range = document.createRange();
range.selectNode(copyDOM);
window.getSelection().addRange(range);
var successful = document.execCommand('copy');
try{
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copy email command was ' + msg);
}catch(err) {
console.log('Oops, unable to copy');
}
window.getSelection().removeAllRanges();
},false);
</script>
</html>