之前在用js查找很多点击复制到剪贴板的实现, 都不能很好的兼容不同的浏览器,下面我们用js和flash来实现复制代码到剪贴板兼容所有浏览器的代码如下。
1, 引入js 和flash 文件.
ZeroClipboard.js
ZeroClipboard.swf
2, 实现代码
<!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/FLASH实现复制代码到剪贴板并且兼容所有浏览器</title>
<script type="text/javascript" src="/kindeditor-4.1.10/copy/ZeroClipboard.js"></script>
<script type="text/javascript" src="/lib/js/jquery.js"></script>
</head>
<body>
text: <input type="text" name="text" value="www.58code.cn 文本" id="text"/>
<input type="submit" name="btn_copy" value="复制1" id="btn_submit" />
<script type="text/javascript">
var clip = new ZeroClipboard.Client();
clip.setHandCursor(true);
var text= document.getElementById('text').value;
clip.setText(text);
clip.glue("btn_submit");
</script>
</body>
</html>