文本编辑器: 运行代码,复制到剪贴板,另存为
<script type="text/javascript">
$ = function (id){
return document.getElementById(id);
}
function copycode(obj){
window.clipboardData.setData("Text",obj.value);
alert("代码复制成功!")
}
function oCopy(objname){
var obj = $(objname);
obj.select();
js=obj.createTextRange();
js.execCommand("Copy");
alert("源码爱好者提示:代码已经被成功复制!");
}
var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
Array.prototype.push = function(value) {
this[this.length] = value;
return this.length;
}
function $(objname){return document.getElementById(objname);}
function runCode(objname) {
var winname = window.open('', "_blank", '');
var obj = $(objname);
winname.document.open('text/html', 'replace');
winname.opener = null // 防止代码对论谈页面修改
winname.document.write(obj.value);
winname.document.close();
}
function saveCode(objname){
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
var obj = $(objname);
winname.document.write(obj.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
</script>
<textarea style='padding: 3px; font-family: "Courier New",Courier,monospace;' class="pt" cols="96" rows="12" id="content" name="content">
<!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复制到剪切板</title>
</head>
<script>
function showDivLinkCopy() {
var msg=document.getElementById("text").value;
window.clipboardData.setData("Text",msg);
}
</script>
<body>
<input name="" type="text" id="text" value="点击右边的复制" style=" border:1px solid #ccc; padding:4px; font-size:14px; color:#444; width:200px; line-height:20px;" />
<a href="javascript:void(0);" onclick="showDivLinkCopy()" style="padding-left:15px; line-height:30px; font-size:16px;">
复制</a>
</body>
</html>
</textarea>
<p class="mtn">
<input onclick="runCode('content')" name="Submit" value="运行代码" type="button">
<input onclick="$('content').select();" name="Submit3" value="全选代码" type="button">
<input onclick="oCopy('content')" name="Submit4" value="复制代码" type="button">
<button onclick="copycode($('content'))" >
<em>
复制代码 js : copycode</em>
</button>
<input onclick="saveCode('content')" name="Submit2" value="保存代码" type="button">
</p>