今天上午一个朋友找我要运行的代码,说是在网上找了好多,但是都有问题,运行不了。
下面简单总结了一下,给用的着的朋友:
<
script
language
="JavaScript"
type
="text/JavaScript"
>
//运行文本域代码
function runCode(obj) {
var newWin=window.open('',"_blank",'');
newWin.document.open('text/html','replace');
newWin.opener=null // 防止代码对论谈页面修改
var Code=document.getElementById(obj).value;
newWin.document.write(Code);
newWin.document.close();
}
//复制文本域代码
function copyCode(obj) {
var rng = document.body.createTextRange();
var Code=document.getElementById(obj)
rng.moveToElementText(Code);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
//另存代码
function saveCode(obj) {
var newWin=window.open('','_blank','top=10000');
newWin.document.open('text/html','replace');
var Code=document.getElementById(obj).value;
newWin.document.write(Code);
newWin.document.execCommand('saveas','','code.htm');
newWin.close();
}
</ script >
//运行文本域代码
function runCode(obj) {
var newWin=window.open('',"_blank",'');
newWin.document.open('text/html','replace');
newWin.opener=null // 防止代码对论谈页面修改
var Code=document.getElementById(obj).value;
newWin.document.write(Code);
newWin.document.close();
}
//复制文本域代码
function copyCode(obj) {
var rng = document.body.createTextRange();
var Code=document.getElementById(obj)
rng.moveToElementText(Code);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
//另存代码
function saveCode(obj) {
var newWin=window.open('','_blank','top=10000');
newWin.document.open('text/html','replace');
var Code=document.getElementById(obj).value;
newWin.document.write(Code);
newWin.document.execCommand('saveas','','code.htm');
newWin.close();
}
</ script >
再用onClick分别引用就可以了,例:onClick="runCode('txtCode')"