js 实现将数据放至系统剪贴板

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Js复制代码 </title>
</head>
<body>
	<input id='biao1' value="你好.要copy的内容!">
	<input type="button" onclick="oCopy();" value='hhhh'/>
<script language="javascript">
function oCopy(){ 
	var obj=document.getElementById("biao1");
obj.select();
document.execCommand("Copy") 
}
</script>
	</body></html>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Js复制代码 js模拟选中,按键粘贴(可用于隐藏域粘贴)</title>
</head>
<body>
<p>啊啊啊</p>
<button id="btn">点击复制</button>
<script type="text/javascript">
var btn = document.getElementById("btn");
btn.addEventListener('click', copyContent);

function copyContent () {
  let val = document.querySelector("p").innerText; // 要复制的内容
  document.addEventListener('copy', save); // 监听浏览器copy事件
  document.execCommand('copy'); // 执行copy事件,这时监听函数会执行save函数。
  document.removeEventListener('copy', save); // 移除copy事件

  // 保存方法
  function save(e) {
    e.clipboardData.setData('text/plain', val); // 剪贴板内容设置
    e.preventDefault();
  }
}
</script>
	</body></html>
	

 

该方法实现js模拟选中,按键粘贴(可用于隐藏域粘贴) ,比如,表格相关数据列数据需复制,此时将数据放入隐藏域中,对该隐藏域进行复制,即可复制成功

法二:使用插件clipboard

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值