效果如下图,点击按钮执行代码之后会生成右边页面
接下来我做一个简单的布局
<style type="text/css">
textarea{
width: 500px;
height: 500px;
}
</style>
<body>
<textarea name="" rows="" cols="" id="text">
</textarea>
<input type="button" name="" id="btn" value="在线运行代码" />
</body>
<script>
window.onload=function(){
var oText=document.getElementByID('text');
var oBtn=document.getElementByID('btn');
oBtn.onclick=function(){
var new=window.open('about:blank');//返回值是一个新的空白document;
new.document.write(oText.value);//将文本域里面的代码添加到空白的document上,这样通过浏览器打开的新的页面就是new里面的代码生成的;
}
}
</script>