【发布时间】:2021-04-12 05:01:32
【问题描述】:
document.write 似乎可以自己弄清楚何时写入头部以及何时写入主体。
例如,它将<style> 和<script> 写入头部,将<div> 写入主体。
是否可以避免这种自动行为并让程序员决定去哪里?
我用 iframe 对此进行了实验。不幸的是,这里的内部代码框似乎不支持与 iframe 交互,所以请看in JSFiddle。
var theiframe=document.getElementById('testing'), output=document.getElementById('output'), thebody=document.getElementById('output_body'), thehead=document.getElementById('output_head'), thebody=document.getElementById('output_body');
console.clear();
console.group('Testing');
theiframe.contentWindow.document.open();
theiframe.contentWindow.document.writeln("<script>var foo1='head';<\/script>");
theiframe.contentWindow.document.writeln("<script>var foo2='body';<\/script>");
theiframe.contentWindow.document.close();
console.log(theiframe.contentWindow.document.documentElement);
thehead.value = theiframe.contentWindow.document.head.innerHTML;
thebody.value = theiframe.contentWindow.document.body.innerHTML;
console.groupEnd();
textarea {width: 50%}
Iframe:
<br /><iframe id="testing"></iframe>
<hr>
Iframe's head:
<br /><textarea id="output_head"><</textarea>
<hr>
Iframe's body:
<br /><textarea id="output_body"><</textarea>
【问题讨论】:
-
我知道,我知道,但这只是更大项目的一小部分,无法更改。
标签: javascript dom iframe