js实现点击复制
mdn参考此处
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<div class="text">555588889999</div>
<div class="copyBtn">点击复制</div>
</head>
<body>
</body>
<script>
function $(tag){
return document.querySelector(tag)
}
const my_text = $(‘.text‘)
const my_copyBtn = $(‘.copyBtn‘)
my_copyBtn.onclick = function(){
console.log(my_text.innerText);
navigator.clipboard.writeText(my_text.innerText).then(()=>{
alert(‘复制成功‘)
}).catch(()=>{
alert(‘复制失败‘)
})
}
</script>
</html>
js实现点击复制,将内容复制至剪贴板