网页中经常要用到"运行代码"功能,其实这个功能实现很简单.
首先,在HTML模式输入下列代码:
<TEXTAREA cols="60" rows="8" id="temp64429">
中间是要运行的代码
</TEXTAREA><br/><INPUT onclick="runEx('temp64429')" type="button" class="ubbbutton" value="运行此代码"/> <INPUT onclick="doCopy('temp64429')" type="button" class="ubbbutton" value="复制此代码"/> [ 可先修改部分代码 再运行查看效果 ]
然后,建立一个JS文件,内容如下:
//运行代码
function runEx(cod1) {
cod=document.all(cod1)
var code=cod.value;
if (code!=""){
var newwin=window.open('','',''); //打开一个窗口并赋给变量newwin。
newwin.opener = null // 防止代码对论谈页面修改
newwin.document.write(code); //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
newwin.document.close();
}
}
//复制代码
function doCopy(ID) {
if (document.all){
textRange = document.getElementById(ID).createTextRange();
textRange.execCommand("Copy");
}
else{
alert("此功能只能在IE上有效")
}
}
好了,现在你也可以在你的网站中添加运行代码功能了!