2016-07-04 36 views
0

我有这样的代码,我想每一个点击iframe的内容之后将是无效,后来才我怎样才能定义iframe有空内容?

<div id="code" contenteditable></div> 
<iframe id="output"></iframe> 
<button onclick="update()">run</button> 

<script> 
function update() { 
var frame = document.getElementById("output"); 
var frameDoc = frame.contentDocument || frame.contentWindow.document; 
var w = document.getElementById("code").textContent; 

//************************************ 
//here is what i want to change 
frame.contentWindow === null; 
frame.contentWindow.document.write(w); 
//************************************ 
} 
</script> 
<style> 
#code{ 
width:47%; 
height:81.8%; 
border:1px solid; 
} 
#output{ 
width:47%; 
height:80%; 
border:1px solid; 
position:absolute; 
right:0px; 
top:0px; 
} 

</style> 

非常感谢你的帮助( - ;

PS-我试图让我的编辑

+0

你需要做的'frame.contentWindow.document.close();'后每次写,那么接下来的写操作将清除。另外为什么不使用frameDoc现在你有它?如果不是,那么一些浏览器会提交页面 – mplungjan

+0

什么是frameDoc? –

+1

您自己的var:var frameDoc = frame.contentDocument || frame.contentWindow.document;' – mplungjan

回答

1

你需要做的

frame.contentWindow.document.close(); 

每次写之后,那么下一次写会清除它。

另外为什么不使用frameDoc现在你有它?

function update() { 
    var w = document.getElementById("code").textContent; 
    var frame = document.getElementById("output"); 
    var frameDoc = frame.contentDocument || frame.contentWindow.document; 
    frameDoc.write(w); 
    frameDoc.close(); 
} 

也使按钮type="button一些浏览器将提交页面,如果不

+0

谢谢!它的工作! –