javascript 拷贝节点cloneNode()使用介绍_javascript技巧

WBOY
Release: 2016-05-16 16:53:27
Original
1619 people have browsed it

cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝
true:表示执行深拷贝,复制本节点以及整个子节点树.
false:浅拷贝.只复制节点本身.
复制后返回的节点副本属于文档所有,但是并没有父节点.除非使用appendChild,insertChild(),replaceChild()将它添加到文档

复制代码代码如下:


1
2
3

var oDiv = document.getElementById("guoDiv");
var deepList = oDiv.cloneNode(true); //拷贝子节点
alert(deepList.childNodes.length); //3或7(兼容性问题,所以结果不一样)
var showList = oDiv.cloneNode(false); //只拷贝当前元素的引用
alert(showList.childNodes.length); //0
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!