在博客中加入运行代码功能

前言在博客中加入运行代码功能。博客园默认的编辑器只有code功能,没有代码运行功能,不利于一些demo的展示。从网上找到了这个方法,不敢私藏,拿来分享。

 

【先看个例子】

<!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"><!-- http://topic.csdn.net/u/20090407/17/f4e03fe8-7085-441a-91c7-d34b088ca502.html --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档 </title> <style type="text/css"> <!-- *{padding:0;margin:0} .pop{z-index:2;background-color:#FF0000;position:absolute;left:40%;top:40%;width:300px;height:100px;display:none} --> </style> </head> <body style="width:100%;height:100%"> <!--首先设置一个层:--> <div id="pop1" style="z-index:1;background-color:#CCCCCC;filter: alpha(opacity=80);width:100%;height:100%;position:absolute;left:0px;top:0px;display:none"> </div> <div id="pop2" class="pop">你好 </div> <!--弹出层的按钮:--> <button οnclick="show('pop1','pop2')">弹出按钮 </button> <script type="text/javascript"> function show(o1,o2){ var o1 = document.getElementById(o1); var o2 = document.getElementById(o2); o1.style.width = document.documentElement.scrollWidth; o1.style.height = document.documentElement.scrollHeight o1.style.display = "block"; o2.style.display = "block"; } </script> <div style="height:999px;width:2999px;"></div> </body> </html>

 

JS配置在新博客中切换到"HTML",在代码里面插入:

ExpandedBlockStart.gif 代码
< script language = " JavaScript "  type = " text/JavaScript " >
// 运行文本域代码
function  runEx(cod1) {
  cod
= document.all(cod1)
  
var  code = cod.value;
  
if  (code != "" ){
    
var  newwin = window.open( '' , '' , '' );  // 打开一个窗口并赋给变量newwin。
    newwin.opener  =   null   //  防止代码对论谈页面修改
    newwin.document.write(code);  // 向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
    newwin.document.close();
  }
}
< / script>

 

 

 

前台调用在新博客中切换到"HTML",在代码里面插入:

< textarea  id ="rn01"  name ="textarea"  rows ="10"  cols ="60" ></ textarea >   < br  />
< input  style ="cursor: hand"  onclick ="runEx('rn01')"  type ="button"  value ="运行代码" >

 

 
 本来想在配置里面的"页首HTML"里面加那段JS的,这样就不用每次发文文章都引用了,但是系统会把 window.open( '' , '' , '' ); 这句自动删除应该是为了防止恶意脚本吧,所以现在就在每篇文章里面加一段了,将就用用了,如果大家有什么更好的方法,欢迎留言。

 

 

 

 

转载于:https://www.cnblogs.com/waiting4u/archive/2009/12/15/1624590.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值