1. 程式人生 > >JS複製內容到剪貼簿(相容FF/Chrome/Safari所有瀏覽器)

JS複製內容到剪貼簿(相容FF/Chrome/Safari所有瀏覽器)

<!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=utf-8" />
<title>js程式碼ZeroClipboard元件複製剪下板相容firefox,Chrome,IE</title>
</head>
 
<body>
 
<style type="text/css"> 
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{font:12px/180% Arial, Helvetica, sans-serif ,"新宋體";}
.demo{width:680px;margin:40px auto 0 auto;}
.demo td{padding:5px;}
#copy-button{width:125px;height:45px;cursor:pointer;border:0;background:url(images/btn.gif) no-repeat;}
</style>
 
<div class="demo">
<table width="100%">
<tr>
<td>
<textarea id="copytext" name="codeiframe" rows="5" cols="60">複製的內容。。。</textarea>
</td>
</tr>
<tr>
<td><input type="button" value="" id="copy-button" /></td>
</tr>
</table>
</div>
 
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/ZeroClipboard.js"></script>
<script type="text/javascript"> 
var clip = null;
ZeroClipboard.setMoviePath("swf/ZeroClipboard.swf");
$(document).ready(function(){
clip = new ZeroClipboard.Client();  
clip.setHandCursor(true);  
clip.setText($("#copytext").val());  
clip.glue("copy-button");
clip.addEventListener("complete", function(){
alert("程式碼已複製到剪貼簿!");
});
});
</script>
 
</body>
</html>