Bud*_*ril 1 javascript document.write
我有一些关于javascript行为的问题(我现在正在学习来自w3schools的javascript)并且我看到了两个带有简单代码的例子,但我不明白为什么代码表现不同:第一个例子:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html> Run Code Online (Sandbox Code Playgroud)
第二个例子:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<button onclick="document.write(5 + 6)">Try it</button>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
为什么在第二个示例中,所有文档内容都替换为"11",并且在第一个示例"11"中附加到文档中?脚本执行时有区别吗?
Ps:我知道这不是一个正确的问题,但是如果你知道一本更好的书或教程来学习javascript,请把它放在评论中(我是ac #backend developer和ex android developer).
这是因为在第一个例子中,浏览器不会document.open自动调用,但是第二个实例.
这是mdn document.write中的单词
如果document.write()调用直接嵌入HTML代码中,那么它将不会调用document.open().
基本上document.open只是清除文档中的所有内容.
检查这些文件document.write和document.open
mdn document.write
mdn document.open
谢谢伙计!好问题!之前没有注意到这一点.