document.body.innerHTML = "" 是否清除网页?
当我在 FF 3.0 中刷新下面的页面时,我希望网页能够清除,但事实并非如此。
为什么 document.body.innerHTML = ""
不清除页面?
更新: 我试图在加载新页面时刷新期间清除上一个屏幕。我实际上想看到页面清晰,等待,然后运行下一个js。我不想在页面加载后清除屏幕。
...
<body>
<script type="text/javascript">
document.body.innerHTML = "";
for (var i = 0; i < 1000000000; i++) {
}
</script>
<img src="images/web.gif" /><br />
<script type="text/javascript">
document.write( "hello<br />");
</script>
<img src="images/warning.png" /><br />
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
document.body.innerHTML = '';
确实清除了body
,是的。但它会清除代码运行时的innerHTML
。当您在图像和脚本实际位于body
中之前运行代码时,它会尝试清除body
,但没有任何内容需要清除。如果要清除
body
,则必须在body
填充内容后运行代码。您可以通过将块放置为
body
的最后一个子级来完成此操作,以便在运行代码之前加载所有内容,或者您必须使用一些监听 dom:loaded 事件的方法。document.body.innerHTML = '';
does clear thebody
, yeah. But it clears theinnerHTML
as it is at the moment the code is ran. As you run the code before the images and the script are actually in thebody
, it tries to clear thebody
, but there's nothing to clear.If you want to clear the
body
, you have to run the code after thebody
has been filled with content. You can do this by either placing the<script>
block as the last child ofbody
, so everything is loaded before the code is ran, or you have to use some way to listen to thedom:loaded
event.要详细说明 Douwem 的答案,请在脚本标记中将代码放入 onload 中:
To expound on Douwem's answer, in your script tag, put your code in an onload:
虽然同意 Douwe Maan 和 Erik 的答案,但这里还有一些其他内容可能对您有用。
首先,在 head 标签中,您可以引用一个单独的 JavaScript 文件,然后该文件是可重用的:
其中 common.js 是名为 common 的顶级目录中的可重用函数文件。
其次,您可以使用setTimeout延迟脚本的操作,例如:
第二个参数以毫秒为单位。我提到这一点是因为您似乎试图延迟原始代码片段中的某些内容。
Whilst agreeing with Douwe Maan and Erik's answers, there are a couple of other things here that you may find useful.
Firstly, within your head tags, you can reference a separate JavaScript file, which is then reusable:
where common.js is your reusable function file in a top-level directory called common.
Secondly, you can delay the operation of a script using setTimeout, e.g.:
The second argument is in milliseconds. I mention this, because you appear to be trying to delay something in your original code snippet.
正如其他人所说,一个简单的解决方案是将脚本放在页面底部,因为所有 DOM 元素都是从上到下同步加载的。否则,我认为您要寻找的是
document.onload(() => {callback_body})
或window.onload(() => {callback_body}) 正如埃里克所说。正如 Douwe Maan 所说,这些允许您在触发 dom:loaded 事件时执行脚本。不确定
window.onload
的属性,但document.onload
仅在所有元素、css 和脚本加载后才会触发。对于您的情况:或者,如果您使用的是旧浏览器:
As others were saying, an easy solution is to put your script at the bottom of the page, because all DOM elements are loaded synchronously from top to bottom. Otherwise, I think what you're looking for is
document.onload(() => {callback_body})
orwindow.onload(() => {callback_body})
as Erik said. These allow you to execute you script when thedom:loaded
event is fired as Douwe Maan said. Not sure about the properties ofwindow.onload
, butdocument.onload
is triggered only after all elements, css, and scripts are loaded. In your case:or, If you are using an old browser:
我不确定你想要实现什么,但你可能想考虑使用 jQuery,它允许你将代码放在 head 标签或单独的脚本文件中,并且在 DOM 加载后仍然执行代码。
然后,您可以执行以下操作:
以及有选择地删除元素(所有 DIV、仅具有某些类的 DIV 等)
I'm not sure what you're trying to achieve, but you may want to consider using jQuery, which allows you to put your code in the head tag or a separate script file and still have the code executed after the DOM has loaded.
You can then do something like:
as well as selectively removing elements (all DIVs, only DIVs with certain classes, etc.)
使用
document.body.innerHTML = '';
确实有效!只是说,如果使用HTML
(DOM
或在function
上),您可以使用document.writeln('');
但只有onClick
或onDoubleClick
:)Using
document.body.innerHTML = '';
does work! Just saying, if usingHTML
(DOM
or onfunction
) you can usedocument.writeln('');
but onlyonClick
oronDoubleClick
:)我使用以下功能清除屏幕
I clear my screen using is function that