html代码:
<script src="ViewCode.js" type="text/javascript"></script>
<textarea id="runcode0" style="width: 600px; height: 260px;"><a href="#" onfocus="this.blur()">去掉点击链接时出现的虚线</a></textarea>
<br>
<input value="运行代码" id="0" onclick="runCode(this.id)" type="button">
<input value="复制代码" id="0" onclick="doCopy(this.id)" type="button">
<input value="保存代码" id="0" onclick="saveCode('runcode0','网页效果')" type="button">
ViewCode.js:
//获取对象
function $(id)
{
if(document.getElementById)
{
return document.getElementById(id);
}
else if(document.all)
{
return document.all[id];
}
else if(document.layers)
{
return document.layers[id];
}
else
{
return null;
}
}
//运行框操作
function createID(DivID)
{
var objs=$(DivID).getElementsByTagName('textarea');
var inps=$(DivID).getElementsByTagName('input');
var buts=$(DivID).getElementsByTagName('button');
var labs=$(DivID).getElementsByTagName('label');
for(var i=0;i<objs.length;i++)
{
objs[i].id="runcode"+i;
inps[i].id=i;
buts[i].id=i;
labs[i].id=i;
}
}
function runCode(obj)
{
var code=$("runcode"+obj).value;
var newwin=window.open('','','');
newwin.opener=null;
newwin.document.write(code);
newwin.document.close();
}
//复制代码
function doCopy(obj)
{
if(document.all)
{
textRange=$("runcode"+obj).createTextRange();
textRange.execCommand("Copy");
alert("代码已经复制到剪切板");
}
else
{
alert("此功能只能在IE上有效/n/n请在文本域中用Ctrl+A选择再复制")
}
}
//另存代码
function saveCode(obj,title)
{
var winname=window.open('','','width=0,height=0,top=200,left=200px');
winname.document.open('text/html','replace');
winname.document.write($(obj).value);
winname.document.execCommand('saveas','',title+'.html');
winname.close();
}