【发布时间】:2015-02-09 12:41:57
【问题描述】:
我只是在尝试实现自己的尝试页面作为 w3schools,它能够让我获取 html 和 js 代码并在 iframe 中运行它
这是我的代码
<html><body>
<script>
function update()
{document.getElementById('display').contentDocument.documentElement.innerHTML = document.getElementById('mycode').value;
}
</script>
<input onclick="update()" type="button" value="Update page" sandbox>
<textarea id="mycode"></textarea>
<iframe id="display" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe></body></html>
但是当我在 textarea 中设置我的代码时,它不会运行任何脚本 我尝试了沙盒属性,但没有结果
【问题讨论】:
-
需要引用iframe的
contentWindow属性,也就是iframe的window。有了它,你就可以操作沙盒化的 DOM。
标签: javascript html iframe