Javascript document.write 清除页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10873942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:22:11  来源:igfitidea点击:

document.write clears page

javascripthtmljavascript-events

提问by C graphics

Why in the code below upon the call of document.writein the function validator()the form elements (the checkbox and button) are removed from screen?

为什么在下面的代码中调用document.write函数时validator()表单元素(复选框和按钮)从屏幕上删除?

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            function validator() {
                if (document.myForm.thebox.checked)
                    document.write("checkBox is checked");
                else 
                    document.write("checkBox is NOT checked");
            }
        </script>
    </head>
    <body>
        <form name="myForm">
            <input type="checkbox" name ="thebox"/>
            <input type="button" onClick="validator()" name="validation" value="Press me for validation"/>
        </form>
    </body>
</html>

回答by Jerome WAGNER

document.write()is used to write to the document stream.

document.write()用于写入文档

In your case, the stream is most probably already closed when the onClick handler is called, because your document has finished loading.

在您的情况下,当调用 onClick 处理程序时,流很可能已经关闭,因为您的文档已完成加载。

Calling document.write()on a closed document stream automatically calls document.open(), which will clear the document.

调用document.write()关闭的文档流会自动调用document.open(),这将清除文档。

回答by Andy Jones

A document.write()called after the page loads will overwrite the current document.

一个document.write()页面加载后调用将覆盖当前文档。

You want to set the text of some element within your document. If you add a

您想设置文档中某个元素的文本。如果你添加一个

<p id="message"></p>

to the end of your body, then you can call

到你身体的尽头,然后你可以打电话

document.getElementById("message").innerHTML = "your text here";

Or with the jQuery library

或者使用 jQuery 库

$("#message").html("your text here");

回答by josh3736

Calling document.writeafterthe document has been loaded implicitly calls document.open, which clears the current document. (Compare to calling document.openwhile the page is loading, which inserts the string into the document stream; it does not clear the document.)

document.write文档加载调用隐式调用document.open,它会清除当前文档。(与document.open加载页面时调用相比,它将字符串插入到文档流中;它不会清除文档。)

document.writeis one of the oldest vestiges of ancient JavaScript, and should generally be avoided. Instead, you probably want to use DOM manipluation methods to update the document.

document.write是古代 JavaScript 最古老的遗迹之一,通常应该避免。相反,您可能希望使用 DOM 操作方法来更新文档。

回答by Taha Paksu

you can use

您可以使用

alert("Checkbox is checked");

or if you will be displaying it on page, first create an element with an id "message" (you can write anything you want, but remember, id's have to be unique on the page) like

或者,如果您要在页面上显示它,请首先创建一个带有 id“消息”的元素(您可以编写任何您想要的内容,但请记住,id 在页面上必须是唯一的),例如

<div id="message"></div>

and then use

然后使用

document.getElementById("message").innerHTML = "Checkbox is checked";

or if you are using jQuery:

或者如果您使用的是 jQuery:

$("#message").html("Checkbox is checked");

or if you are using a console enabled browser (Firefox, Chrome etc.)

或者如果您使用的是支持控制台的浏览器(Firefox、Chrome 等)

console.log("Checkbox is checked");

instead of

代替

document.write("Checkbox is checked");

回答by Misha Taylor

function DisplayWrite(a) {
    if (document.body) {
        var x = document.createElement("div")
        document.body.appendChild(x)
        x.innerHTML = "</div>" + a
    } else {
        document.writeclear(a)
    }
}

var document = new Object()
document.writeclear = document.write
document.write = function(x) {DisplayWrite(x)}

回答by Sampo Sarrala - codidact.org

document.writeis not best way to go as if there is any, yes ANY, DOM manipulation happening or happened already then document.write()does not work very well as it modifies original document but ignores any changes to DOM tree.

document.write不是最好的方法,就好像有任何,是的,正在发生或已经发生的 DOM 操作然后document.write()不能很好地工作,因为它修改了原始文档但忽略了对 DOM 树的任何更改。

Also remember that browser displays things primarily from DOM, not from original document.

还请记住,浏览器主要显示来自 DOM 的内容,而不是来自原始文档。