2016-07-04 39 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

謝謝!它的工作! –