From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/document/importnode/index.html | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 files/zh-cn/web/api/document/importnode/index.html (limited to 'files/zh-cn/web/api/document/importnode') diff --git a/files/zh-cn/web/api/document/importnode/index.html b/files/zh-cn/web/api/document/importnode/index.html new file mode 100644 index 0000000000..0f49c8554d --- /dev/null +++ b/files/zh-cn/web/api/document/importnode/index.html @@ -0,0 +1,69 @@ +--- +title: document.importNode +slug: Web/API/Document/importNode +translation_of: Web/API/Document/importNode +--- +

{{ ApiRef() }}

+ +

概述

+ +

将外部文档的一个节点拷贝一份,然后可以把这个拷贝的节点插入到当前文档中.

+ +

语法

+ +
var node = document.importNode(externalNode, deep);
+
+ +
+
node
+
导入当前文档的新节点. 新节点的 parentNodenull, 因为它还没有插入当前文档的文档树中,属于游离状态.
+
externalNode
+
将要从外部文档导入的节点.
+
deep {{ optional_inline() }}
+
一个布尔值,表明是否要导入节点的后代节点.
+
+ +
+

注意: 在DOM4规范中 (实现于Gecko 10.0 {{ geckoRelease("10.0") }}) ,deep 是个可选的参数. 如果省略不写,则使用默认值 true, 表示深度克隆. 如果你想使用浅克隆,则传入false参数.

+ +

在没有实现DOM4的浏览器中, 这个参数不可以省略.

+
+ +

例子

+ +
var iframe = document.getElementsByTagName("iframe")[0];
+var oldNode = iframe.contentDocument.getElementById("myNode");
+var newNode = document.importNode(oldNode, true);
+document.getElementById("container").appendChild(newNode);
+
+ +

备注

+ +

源节点不会从外部文档中删除,被导入的节点是源节点的一个拷贝.

+ +

 

+ +

将外部文档的节点插入当前文档之前,你必须使用 document.importNode() 从外部文档导入源节点,或者使用 document.adoptNode()导入源节点, 想要了解更多的 Node.ownerDocument 问题,请参考 W3C DOM FAQ.

+ +

即使你不执行导入动作,就执行插入外部文档中的节点.Firefox目前也不会报错(如果严格按标准执行,很多已有的网站都无法正常运行). 我们鼓励开发者严格按标准修改自己已有的不符合上述标准的代码.

+ +

 

+ +

浏览器兼容性

+ + + +

{{Compat("api.Document.importNode")}}

+ +

规范

+ + + +

相关介绍

+ + -- cgit v1.2.3-54-g00ecf