美文网首页HTML5
javascript-document.importNode和d

javascript-document.importNode和d

作者: 家里有棵核桃树 | 来源:发表于2018-10-10 18:47 被阅读1次

在利用shadow-dom创建组件的时,偶然发现了document.importNode的使用,所以就想认真学习一下document.importNode和document.adoptNode的使用。

1、document.importNode

1.1 用法

var node = document.importNode(externalNode, deep)

1.2 特性

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

1.3 示例

(1)iframe.html【同2.3示例iframe.html】

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>iframe</title>
</head>
<body>
<h1>iframe标题一-importNode</h1>
<h2>iframe标题二-adoptNode</h2>
</body>
</html>

(2)index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>document.importNode的应用</title>
    <style>
        iframe {
            border: 3px solid #2185d1;
        }
    </style>
</head>
<body>
<iframe src="iframe.html" frameborder="0" width="600" height="200"></iframe>

<script>
    // 要等子页面加载完了再执行 否则获取到的iframeDocument不对
    window.onload = function () {

        const iframeDocument = document.querySelector("iframe").contentDocument;
        const cloneNodeH1 = iframeDocument.querySelector('h1');

        // document.importNode的用法
        let importNode = document.importNode(cloneNodeH1, true); // 深拷贝
        document.body.appendChild(importNode);
        console.log(cloneNodeH1 === importNode); // false
        console.log(importNode.ownerDocument); // 当前文档
        console.log(cloneNodeH1.ownerDocument); // iframeDocument
    };
</script>
</body>
</html>

1.4 效果图

效果图

2、document.adoptNode

2.1 用法

var node = document.adoptNode(externalNode)

2.2 特性

document.adoptNode用途:从其他的document文档中获取一个节点。 该节点以及它的子树上的所有节点都会从原文档删除 (如果有这个节点的话), 并且它的ownerDocument 属性会变成当前的document文档。 之后你可以把这个节点插入到当前文档中。【该方法也同样适用 同一文档的不同元素切换】。

2.3 示例

(1)iframe.html【同1.3示例iframe.html】

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>iframe</title>
</head>
<body>
<h1>iframe标题一-importNode</h1>
<h2>iframe标题二-adoptNode</h2>
</body>
</html>

(2)index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>document.adoptNode的应用</title>
    <style>
        iframe {
            border: 3px solid #2185d1;
        }
    </style>
</head>
<body>
<iframe src="iframe.html" frameborder="0" width="600" height="200"></iframe>

<script>
    // 要等子页面加载完了再执行 否则获取到的iframeDocument不对
    window.onload = function () {

        const iframeDocument = document.querySelector("iframe").contentDocument;
        const cloneNodeH2 = iframeDocument.querySelector('h2');

        // document.adoptNode的用法
        let adoptNode = document.adoptNode(cloneNodeH2);
        document.body.appendChild(adoptNode);
        console.log(cloneNodeH2 === adoptNode); // true
        console.log(adoptNode.ownerDocument); // 当前文档
        console.log(cloneNodeH2.ownerDocument); // 当前文档


    };
</script>
</body>
</html>

1.4 效果图

效果图

3、document.adoptNode在同一文档不同元素间的应用

功能:通过点击不同的按钮,实现列表项的左右切换显示
(1)index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>document.adoptNode在同一文档的使用</title>
    <style>
        * {
            margin: 0;
        }
        body, .box {
            padding: 20px;
        }
        .box {
            display: table;
            bottom: 0;
            border: 3px solid #2185d1;
        }
        ul, .btnGroup{
            float: left;
            margin: 20px 50px;
        }
        ul {
            width: 60px;
            height: 150px;
            padding: 20px 50px;
            border: 1px solid #155269;
            border-radius: 5px;
        }
        .btnGroup {
            margin-top: 90px;
        }
        button ~ button {
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div class="box">
    <h3>document.adoptNode同一文档下的应用</h3>
    <ul>
        <li>面包</li>
        <li>包子</li>
        <li>子不语</li>
        <li>语言</li>
    </ul>
    <div class="btnGroup">
        <button type="button" onclick="moveRight()">&gt;&gt;</button>
        <br>
        <button type="button" onclick="moveLeft()">&lt;&lt;</button>
    </div>
    <ul></ul>
</div>
<script>
    const ul1 = document.getElementsByTagName('ul')[0];
    const ul2 = document.getElementsByTagName('ul')[1];

    function move(eleFrom, eleTo) {

        let firshChild = eleFrom.firstElementChild;
        if (null == firshChild) {

            alert('列表为空,不能移动');
            return false;
        }
        eleTo.append(document.adoptNode(firshChild));
    }

    function moveRight() {

        move(ul1, ul2);
    }

    function moveLeft() {

        move(ul2, ul1);
    }
</script>
</body>
</html>

(2)效果图


效果图

4、参考链接

相关文章

  • javascript-document.importNode和d

    在利用shadow-dom创建组件的时,偶然发现了document.importNode的使用,所以就想认真学习一...

  • Kinect学习(1)——DepthBasics-D2D

    参考:DepthBasics-D2D详解之一 和 DepthBasics-D2D详解之二 和 DepthBasic...

  • deferred和promise的理解。

    deferred和promise的关系和区别 1、d对象通过d.resolve()和d.reject()来选择do...

  • CSS3--3D转换

    3D转换 什么是2D和3D2D就是一个平面, 只有宽度和高度, 没有厚度3D就是一个立体, 有宽度和高度, 还有厚...

  • 3D-TOOL V10.05软件工具

    3D-TOOL是查看3d和2d CAD文件的工具,支持导入常见的3d和2d CAD文件格式。 3D-Tool破解版...

  • 22-3D转换模块

    什么是2D和3D2D就是一个平面, 只有宽度和高度, 没有厚度3D就是一个立体, 有宽度和高度, 还有厚度 默认情...

  • CSS-3D转换模块

    什么是2D和3D2D就是一个平面, 只有宽度和高度, 没有厚度3D就是一个立体, 有宽度和高度, 还有厚度默认情况...

  • 3D转换模块

    1.什么是2D和3D2D就是一个平面, 只有宽度和高度, 没有厚度3D就是一个立体, 有宽度和高度, 还有厚度默认...

  • 2D与3D

    前言 说到2d与3d的转换,到底什么是2d,什么又是3d,看下图: 所谓的2d和3d就是我们理解的二维空间和三维空...

  • 11. 3D转换模块 CSS3背景

    3D转换模块 什么是2D和3D2D是一个平面, 有宽度, 没有厚度(默认情况)3D是一个立体, 有宽度和高度, 有...

网友评论

    本文标题:javascript-document.importNode和d

    本文链接:https://www.haomeiwen.com/subject/lbdmaftx.html