document.write清除页面 [英] document.write clears page

查看:106
本文介绍了document.write清除页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascript>
函数validator(){
if(document.myForm.thebox.checked)
document.write(checkBox is checked);
else
document.write(checkbox is checked checked);
}
< / script>
< / head>
< body>
< form name =myForm>
< input type =checkboxname =thebox/>
< input type =buttononClick =validator()name =validationvalue =Press me for validation/>
< / form>
< / body>
< / html>


解决方案

document.write() 用于写入文档 stream



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



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


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

<!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>

解决方案

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

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

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

这篇关于document.write清除页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆