cnblogs 运行代码功能尝试

转自:http://www.jb51.net/article/28005.htm

cnblogs 运行代码功能尝试,使用cnblogs的朋友可以参考下。

 1 <!--定义和用法
 2 
 3 open() 方法可打开一个新文档,并擦除当前文档的内容。
 4 
 5 语法
 6 document.open(mimetype,replace)
 7 参数    描述
 8 mimetype    可选。规定正在写的文档的类型。默认值是 "text/html"。
 9 replace    可选。当此参数设置后,可引起新文档从父文档继承历史条目。
10 说明
11 该方法将擦除当前 HTML 文档的内容,开始一个新的文档,新文档用 write() 方法或 writeln() 方法编写。
12 
13 提示和注释
14 
15 重要事项:调用 open() 方法打开一个新文档并且用 write() 方法设置文档内容后,必须记住用 close 方法关闭文档,并迫使其内容显示出来。
16 
17 注释:属于被覆盖的文档的一部分的脚本或事件句柄不能调用该方法,因为脚本或事件句柄自身也会被覆盖。
18 
19 实例
20 -->
21 <html>
22 <head>
23 <script type="text/javascript">
24 function createNewDoc()
25   {
26   var newDoc=document.open("text/html","replace");
27   var txt="<html><body>Learning about the DOM is FUN!</body></html>";
28   newDoc.write(txt);
29   newDoc.close();
30   }
31 </script>
32 </head>
33 <body>
34 
35 <input type="button" value="Write to a new document"
36 onclick="createNewDoc()">
37 
38 </body>
39 </html>

 

首先定义个文本域并且给个ID
<textarea id="O_txt_1" rows="8" cols="80"> <!--要运行的代码--> </textarea>
然后定义个按钮
<input type="button" value="运行代码" onclick="runCode('O_txt_1')" />
最后当然是要写函数了,代码如下:

1 function runCode(id){ 
2 var obj=document.getElementById(id); 
3 var win = window.open('', "_blank", ''); 
4 win.document.open('text/html', 'replace'); 
5 win.opener = null; 
6 win.document.write(obj.value); 
7 win.document.close(); 
8 } 

posted on 2012-06-14 11:34  jQing  阅读(218)  评论(0编辑  收藏  举报

导航