非window.clipboardData.setData
上面?这个只支持ie 谷歌火狐不支持
直接看代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<span id="tt">sssaaasss</span>
<button onclick="copys()"></button>
<script type="text/javascript">
function copys(){
var dom=document.getElementById("tt");
tt.focus();
var selection;
var rg;
if(window.getSelection){
selection=window.getSelection();
}else{
rg=document.selection.createRange();
}
if(rg){
rg.moveToElementText(dom);
rg.select();
}else{
selection.selectAllChildren(tt);
}
//第二个参数必须为false,不然firfox执行不成功。
document.execCommand("copy", false);
selection?selection.removeAllRanges():rg.collapse();
tt.blur();
}
</script>
</body>
</html>
具体操作打断点跟一下 大致第一步选中文本第二部复制到粘贴板
补充第二种方法
js获取需要复制的内容,然后再页面上设置一个隐藏的input 把获取到的内容赋值给这个input 然后再用JQ的.select()方法
然后再用 document.execCommand(“Copy”); // 执行浏览器复制命令
ok完成