HTML DOM importNode() 方法 | HTML DOM Document 对象

返回到:DOM 对象:HTML DOM Document 对象

定义和用法

importNode() 方法把一个节点从另一个文档复制到该文档以便应用。

imported 节点可以试试任何节点类型。

如果 第二个值设置为 true,那么还要复制该节点的所有子孙节点。

浏览器支持:

所有主要浏览器都支持 importNode() 方法

注意: Internet explorer 8 及 IE 更早版本不支持该方法。

语法

document.importNode(node,deep)

参数

参数类型描述
deepBoolean必须。如果为 true,还要递归复制 importedNode 节点的所有子孙节点。

返回值

类型描述
Node objectThe imported node

技术细节

DOM 版本Core Level 2 Document Object

实例

返回iframe中第一个 H1 元素:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>

<button onclick="myFunction()">点我</button>
<p id="demo">点击上面的按钮以获取和显示在iframe第一个H1元素的值:</p>
<script>
function myFunction(){
	var frame=document.getElementsByTagName("iframe")[0]
	var h=frame.contentWindow.document.getElementsByTagName("h1")[0];
	var x=document.importNode(h,true);
	document.getElementById("demo").appendChild(x);
};
</script>
<iframe src="https://www.web176.com" style="height:280px;width:420px;"></iframe>
<p><strong>Note:</strong> IE 8 及更早的版本不支持importNote方法</p>

</body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4639.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年12月15日 下午2:32
下一篇 2021年12月15日 下午2:37

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注