当前位置:网站首页>Cloning and importing DOM nodes
Cloning and importing DOM nodes
2022-06-25 06:39:00 【InfoQ】
Preface
node.cloneNode()
node.cloneNode()grammar
let cloneNode = targetNode.cloneNode(deep);
- cloneNode The node copy generated by the final Clone .
- targetNode The target node to be cloned .
- deep Optional parameters , Indicates whether deep cloning is required , That is, whether you need to clone targetNode Child nodes under , The default is false.
<body>
<div id="container">
<div class="header"> This is the head </div>
<div class="body">
<div class="content"> Content of a </div>
<div class="content"> Content of the two </div>
</div>
</div>
<script>
const target = document.querySelector(".body");
const cloneNode1 = target.cloneNode();
console.log("cloneNode1.outerHTML\n\n",cloneNode1.outerHTML);
const cloneNode2 = target.cloneNode(true);
console.log("cloneNode2.outerHTML\n\n", cloneNode2.outerHTML);
</script>
</body>

document.importNode()
let node = document.importNode(externalNode, deep);
- node The node object imported externally into the current document .
- externalNode The target node to be imported in the external document .
- deep Deep copy or not , The default is false.
<!--iframe.html-->
<body>
<h1> This is a Iframe page </h1>
<div id="container">
<div class="header"> This is a Iframe Content header </div>
<div class="body"> This is a Iframe Content subject </div>
</div>
</body>
<!--index.html-->
<body>
<div id="container">
<div class="header"> Content header </div>
<div class="body"> Content subject </div>
</div>
<iframe id="iframe_ele" src="./iframe.html"></iframe>
<script>
window.onload = function () {
const iframeEle = document.getElementById('iframe_ele');
const iframeContainer = iframeEle.contentDocument.getElementById("container");
const importedNode = document.importNode(iframeContainer, true);
document.body.appendChild(importedNode);
}
</script>
</body>

document.adoptNode()
ownerDocumentlet node = document.adoptNode(externalNode);
- node Node object obtained from external document .
- externalNode The node object in the external document to be imported .
<!--iframe.html-->
<body>
<h1> This is a Iframe page </h1>
<div id="container">
<div class="header"> This is a Iframe Content header </div>
<div class="body"> This is a Iframe Content subject </div>
</div>
</body>
<!--index.html-->
<body>
<div id="container">
<div class="header"> Content header </div>
<div class="body"> Content subject </div>
</div>
<iframe id="iframe_ele" src="./iframe.html"></iframe>
<script>
window.onload = function () {
const iframeEle = document.getElementById('iframe_ele');
const iframeContainer = iframeEle.contentDocument.getElementById("container");
const node = document.adoptNode(iframeContainer);
document.body.appendChild(node);
}
</script>
</body>

summary
边栏推荐
- Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things
- How to use asemi FET 7n80 and how to use 7n80
- Kotlin reflection -- Notes
- What is VLAN
- Missing libgmp-10 dll - libgmp-10. dll is missing
- How to create a handy vs Code?
- JD 7 head search navigation layout
- From file system to distributed file system
- PHP converts strings to hex and hex to string - PHP convert string to hex and hex to string
- Analysis of China's food cold chain logistics, output of quick-frozen noodles and rice products and operation of major enterprises in 2021 [figure]
猜你喜欢

keil debug查看变量提示not in scope

@Detailed explanation of valid annotation usage

DNS domain name system

2022-02-19: fence installation. In a two-dimensional garden, there are some trees represented by (x, y) coordinates. As the installation cost is very expensive, your task is to enclose all the trees w

General test point ideas are summarized and shared, which can be directly used in interview and actual software testing

Microsoft issued a document to celebrate Net 20th anniversary!

Exercise: completion

Derivation of sin (a+b) =sina*cosb+sinb*cosa

Cs4344/ht5010 stereo d/a digital to analog converter

Uncaught TypeError: Cannot read properties of undefined (reading ‘prototype‘)
随机推荐
[轻松学会shell编程]-5、计划任务
Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things
Uncaught typeerror cannot set properties of undefined (setting 'classname') reported by binding onclick event in jsfor loop
SAP QM executes the transaction code qp01, and the system reports an error -material type food is not defined for task list type Q-
cos(a-b)=cosa*cosb+sina*sinb的推导过程
[core content and derivation] the mystery of human memory system may be just like this
Is the number of indexes in a table the more the better?
How to realize hierarchical management of application and hardware in embedded projects
有能力的人从不抱怨大环境!
アルマ / 炼金妹
Preliminary practice of niuke.com (summary)
How to create a handy vs Code?
Kotlin reflection -- Notes
Wechat applet simply realizes chat room function
General test point ideas are summarized and shared, which can be directly used in interview and actual software testing
Understand what MSS is
Sophomores majoring in mechanics build a manipulator by hand -- full of compromise
JD 7 head search navigation layout
From file system to distributed file system
Ht81293 built in adaptive dynamic boost 20W mono class D power amplifier IC solution