jq 点击复制div里面的内容 如果粘贴到富文本中,会将样式,里面所有的标签,文字一并粘贴进去

时间:2023-01-04 06:15:28
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>点击复制功能</title>
</head>
<script src="http://code.jquery.com/jquery-2.0.2.min.js"></script>
<style>
.popup__bottom__text{
margin-bottom: 10px;
}
</style>
<body>
<div class="popup__bottom__text">
<a id="text_1" style="font-size:12px; color:#0066cc;" href="/ueditor/php/upload/file/20181016/1539677909627575.png" title="1539677909627575.png">bbbb.png</a>
<a href="javascript:(0)" onclick="tapCopy('text_1')">点我复制</a>
</div>
<div class="popup__bottom__text">
<a id="text_2" style="font-size:12px; color:#0066cc;" href="/ueditor/php/upload/file/20181016/1539677909627575.png" title="1539677909627575.png">aaaa.png</a>
<a href="javascript:(0)" onclick="tapCopy('text_2')">点我复制</a>
</div> </body>
</html>
<script>
//复制文本
function tapCopy($id) {
     //var $id = $(dom).prev().attr('data_id');
selectText($id);
document.execCommand('copy');
alert('复制成功');
}
//选中文本
function selectText(element) {
var text = document.getElementById(element);
//做下兼容
if (document.body.createTextRange) { //如果支持
var range = document.body.createTextRange(); //获取range
range.moveToElementText(text); //光标移上去
range.select(); //选择
} else if (window.getSelection) {
var selection = window.getSelection(); //获取selection
var range = document.createRange(); //创建range
range.selectNodeContents(text); //选择节点内容
selection.removeAllRanges(); //移除所有range
selection.addRange(range); //添加range
/*if(selection.setBaseAndExtent){
selection.setBaseAndExtent(text, 0, text, 1);
}*/
} else {
alert("复制失败");
}
} </script>