点击某个内容复制到粘贴板

<!DOCTYPE html>

<head>
<title>中英文切换</title>
<meta charset="UTF-8"/>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="language.js"></script>
</head>

<body>
<table id="data">
<tr>
<td class="copyable"> 姓名:</td>
<td class="copyable">张三</td>
</tr>
<tr>
<td> 年龄:</td>
<td>18</td>
</tr>
<tr>
<td class="copyable"> 性别:</td>
<td class="copyable">男</td>
</tr>
</table>
<br>
<p class="copy">copy</p>
<p class="copy">copy2</p>
<br>
<div id="copy-temp"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$(".copy").click(function () {
var $o = $("<textarea rows='10' cols='50'>");
$o.val($(this).text())//填充要复制的文字
$("#copy-temp").append($o)
if (copy($o[0])) {
$o.remove()
alert("复制成功!");
} else {
alert("复制失败,请手动复制");
}
})

function copy(obj) {
obj.select();
try {
if (document.execCommand('copy', false, null)) {
document.execCommand("Copy");
return true;
}
} catch (err) {

}
return false;
}
</script>
posted on 2019-07-08 09:41  你们都厉害  阅读(387)  评论(0编辑  收藏  举报