【问题标题】:Will `document.appendChild()` reload all the html page?`document.appendChild()` 会重新加载所有的 html 页面吗?
【发布时间】:2018-11-30 15:32:01
【问题描述】:

document.appendChild(xxx)会重新加载所有的html页面吗?

我有两个问题:

  1. 当我使用document的功能时,会重新加载所有的html页面吗?
  2. window 对象是否属于 JavaScript?以及是否通过 JavaScript 方法改变一个 html 页面的一部分,所有的页面都会重新加载?

【问题讨论】:

  • document.appendChild() 会抛出错误

标签: javascript dom window document


【解决方案1】:

实际上你不能使用 document.appendChild() 因为文档只能有一个元素。例如,如果您在浏览器中打开浏览器控制台并尝试运行:

document.appendChild(document.createElement('span'));

会报错:

VM912:1 Uncaught DOMException: Failed to execute 'appendChild' on 'Node': Only one element on document allowed.

但是,您可以将子节点附加到其他 DOM 节点,例如主体

document.body.appendChild(document.createElement('span'));

会将一个新节点附加到 body 标记。它不会重新加载整个页面。如果您在浏览器控制台中运行它,然后查看页面的 HTML 元素,您会看到现在在结束

猜你喜欢
  • 1970-01-01
  • 2020-06-22
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 2021-10-29
  • 2015-08-28
相关资源
最近更新 更多