gpt4 book ai didi

javascript - Document.write 清除页面

转载 作者:技术小花猫 更新时间:2023-10-29 12:08:56 27 4
gpt4 key购买 nike

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

<!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() 用于写入文档

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

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

关于javascript - Document.write 清除页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10873942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com