运行代码框
简单的运行代码框,可以支持多个
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> window.onload=function () { var aCode= getClass('textarea','code'); var oBtn= getClass('input','run'); var i=0; for(i=0;i<aCode.length;i++){ oBtn[i].index=i; oBtn[i].onclick=function () { var oNewWin=window.open('about:blank'); oNewWin.document.write(aCode[this.index].value); }; } }; function getClass(oTag,oClass){ var result=[]; var i=0; aTag=document.getElementsByTagName(oTag); for(i=0;i<aTag.length;i++){ if(aTag[i].className==oClass){ result.push(aTag[i]); } } return result; } </script> </head> <body> <textarea class="code" rows="10" cols="40" style="width:100%;"> test </textarea><br /> <input class="run" type="button" value="运行" /> <textarea class="code" rows="10" cols="40" style="width:100%;"> test </textarea><br /> <input class="run" type="button" value="运行" /> </body> </html>
test test test