JS实现复制到剪贴板

时间:2021-08-26 22:06:23
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>复制到剪贴板</title>
<script src="http://img3.job1001.com/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://img3.job1001.com/js/ZeroClipboard/jquery.zclip.min.js"></script>
</head>
<body>
<input type="text" id="copy_value"/>
<input class="copy_btn" type="button" value="一键复制"/>
<br/><br/>
<input type="text" id="copy_value"/>
<input class="copy_btn" type="button" value="一键复制"/>
<br/><br/>
<input type="text" id="copy_value"/>
<input class="copy_btn" type="button" value="一键复制"/>
<script>
$(document).ready(function(){
if ( window.clipboardData ) {
$('.copy_btn').click(function() {
window.clipboardData.setData("Text", $(this).prev('input').val());
alert('复制成功!');
});
} else {
$(".copy_btn").zclip({
path:'http://img3.job1001.com/js/ZeroClipboard/ZeroClipboard.swf',
copy:function(){return $(this).prev('input').val();},
afterCopy:function(){alert('复制成功!');}
});
}
});
</script>
</body>
</html>