Javascript 用本页面文本域中的HTML代码打开一个空白窗口来运行
生活随笔
收集整理的這篇文章主要介紹了
Javascript 用本页面文本域中的HTML代码打开一个空白窗口来运行
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼:
<!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" > <head><title>打開一個空白頁面</title><!--PageBeginHtml Block Begin--><script type="text/javascript">function runCode(id){var obj = document.getElementById(id);if(obj == null || obj.value == null || obj.value == ""){return;}var TestWin = window.open(''); //打開一個窗口并賦給變量TestWin。TestWin.opener = null; // 防止代碼對論談頁面修改 TestWin.document.write(obj.value); //向這個打開的窗口中寫入代碼code,這樣就實現了運行代碼功能。 TestWin.document.close();}</script><!--PageBeginHtml Block End--> </head> <body> <div style="margin:0px auto;"> <textarea id="txtCode" cols="" rows="" title="請填寫一些HTML代碼" style="width:800px; height:400px;"><!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" > <head><title>打開一個空白頁面</title><script type="text/javascript">function show(){alert("Hello World");}</script> </head> <body> <div id="Div1"><input type="button" value="點擊我看看" οnclick="show();" /> </div> </body> </html></textarea> </div> <div id="header"><input type="button" value="運行" οnclick="runCode('txtCode');" /> </div> </body> </html>
謝謝瀏覽!
轉載于:https://www.cnblogs.com/Music/archive/2011/04/25/javascript-open-textarea-html-code-to-a-new-window.html
總結
以上是生活随笔為你收集整理的Javascript 用本页面文本域中的HTML代码打开一个空白窗口来运行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于web.config
- 下一篇: 2017年html5行业报告,云适配发布