运行代码,复制代码,保存代码

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">

&nbsp;

<input value="复制代码" id="0" onclick="doCopy(this.id)" type="button">

&nbsp;

<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();

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值