请教一个关于chrome下复制事件的代码
1个回答
展开全部
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
<meta name="format-detection" content="telephone=no" />
</head>
<body>
<input type="text" id="inp" value="复制的内容" />
<br/>
1231231231231231
<br/>
<button id="btn" style="width: 100px; height: 50px; background: red; color: #fff; line-height: 50px; text-align: center; border: none;">点击复制到剪切板</button>
<script>
//获取剪贴板数据方法
function getClipboardText(event){
var clipboardData = event.clipboardData || window.clipboardData;
return clipboardData.getData("text");
};
//设置剪贴板数据
function setClipboardText(event, value){
if(event.clipboardData){
return event.clipboardData.setData("text/plain", value);
}else if(window.clipboardData){
return window.clipboardData.setData("text", value);
}
};
var oInp = document.getElementById("inp");
var btn = document.getElementById("btn");
function setClipboardText(event, value){
if(event.clipboardData){
alert(1);
return event.clipboardData.setData("text/plain", value);
}else if(window.clipboardData){
alert(2);
return window.clipboardData.setData("text", value);
}else{
alert(4);
}
};
function abc(){
alert(3);
var event = event || window.event;
setClipboardText(event, oInp.value);
}
window.onload = function() {
oInp.addEventListener(*copy*, abc, false);
btn.addEventListener(*click*, abc, false);
}
</script>
</body>
</html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
<meta name="format-detection" content="telephone=no" />
</head>
<body>
<input type="text" id="inp" value="复制的内容" />
<br/>
1231231231231231
<br/>
<button id="btn" style="width: 100px; height: 50px; background: red; color: #fff; line-height: 50px; text-align: center; border: none;">点击复制到剪切板</button>
<script>
//获取剪贴板数据方法
function getClipboardText(event){
var clipboardData = event.clipboardData || window.clipboardData;
return clipboardData.getData("text");
};
//设置剪贴板数据
function setClipboardText(event, value){
if(event.clipboardData){
return event.clipboardData.setData("text/plain", value);
}else if(window.clipboardData){
return window.clipboardData.setData("text", value);
}
};
var oInp = document.getElementById("inp");
var btn = document.getElementById("btn");
function setClipboardText(event, value){
if(event.clipboardData){
alert(1);
return event.clipboardData.setData("text/plain", value);
}else if(window.clipboardData){
alert(2);
return window.clipboardData.setData("text", value);
}else{
alert(4);
}
};
function abc(){
alert(3);
var event = event || window.event;
setClipboardText(event, oInp.value);
}
window.onload = function() {
oInp.addEventListener(*copy*, abc, false);
btn.addEventListener(*click*, abc, false);
}
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询