网页中嵌入可以点击“运行代码”执行html/css/js代码

html代码

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

JS代码

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

 效果展示:


点击“运行代码”

原文地址:https://www.cnblogs.com/cnsevennight/p/4046419.html