gpt4 book ai didi

javascript - 我如何定义 iframe 具有空内容?

转载 作者:行者123 更新时间:2023-12-03 06:36:19 25 4
gpt4 key购买 nike

我有这段代码,我希望每次点击后 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>

非常感谢您的帮助(-;

附注-我尝试制作我的编辑器

最佳答案

你需要做

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,如果没有,某些浏览器将提交页面

关于javascript - 我如何定义 iframe 具有空内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38185272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com