在“网页上的“运行代码”功能(多区域无ID版)”中给出在网页上添加“运行代码框”的方法和代码,那么如何让网页内其它标记的内容在新窗口中运行呢?
可以用.innerText或.textContent取得标签内容,但是IE只支持innerText属性而Firefox只支持textContent,本文使用.innerHTML和正则结合的方法来模拟innerText属性以实现更大的兼容性。
详细代码清单如下:
<!--HTML部分-->
<SPAN>
<TABLE style="TABLE-LAYOUT: fixed;background:#fffeee;border:1px solid #888" cellSpacing=0 cellPadding=7 width="96%">
<TBODY>
<TR>
<TD style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word">
这里是你要运行的代码
</TD>
</TR>
</TBODY>
</TABLE>
<INPUT onclick=runCode(this) type=button value=运行代码>
</SPAN>
<!--以下是JS脚本-->
<SCRIPT>
function runCode(e){
var ji=0;
while(e.parentNode.childNodes[ji].nodeType!=1)ji++;
var newwin=window.open("","","");
newwin.opener = null;
newwin.document.write(e.parentNode.childNodes[ji].innerHTML.replace(/\s/gi," ").replace(/<.*?>/gi,"\n").replace(/</gi,"<").replace(/>/gi,">").replace(/ /gi," ").replace(/&/gi,"&"));
newwin.document.close();}</SCRIPT>

郭文的最新文章:
