代码之家  ›  专栏  ›  技术社区  ›  ewizard

无法克隆<template>以附加到<div>

  •  0
  • ewizard  · 技术社区  · 7 年前

    我用javascript成功创建了这个模板:

    enter image description here

    我在一个 async 功能:

    this.createBoxes = async function() {
        var row_counter = 0;
        for (var i = 1; i < this.fake_data.length + 1; i++) {
    
            var item_box = document.createElement("div");
            item_box.style.flex = "0.5";
            item_box.style.backgroundColor = "white";
            item_box.style.display = "flex";
            item_box.style.flexDirection = "column";
            item_box.style.justifyContent = "flex-end";
            item_box.id = "item_box_"+i;
    
            var item_name = document.createElement("h3");
            item_name.style.flex = "0.2";
            item_name.style.backgroundColor = "orange";
            item_name.style.alignSelf = "center";
            item_name.innerText = this.fake_data[i - 1].name;
            item_name.id = "item_name_"+i;
    
            item_box.appendChild(item_name);
    
            this_row = document.getElementsByClassName("row")[row_counter];
            this_row.appendChild(item_box);
    
            if(i % 2 == 0) {
                var pool = document.getElementById("pool");
                var inner_row = document.createElement("div");
                inner_row.style.display = "flex";
                inner_row.style.flexDirection = "row";
                inner_row.style.flex = "0.5";
                inner_row.style.justifyContent = "space-around";
                inner_row.style.alignItems = "center";
                inner_row.style.backgroundColor = "green";
                inner_row.className = "row";
    
                pool.appendChild(inner_row);
    
                row_counter++;
            }
            else if(i == this.fake_data.length) {
                return;
            }
        }
    }
    

    然后我会这样做:

        this.createBoxes().then(function() {
            var template = document.querySelector('#pool');
            var clone = template.content.cloneNode(true);
            document.querySelector(".app").appendChild(clone);
        })
    

    但是你可以从我的截图上看到, .app 是空的。我做错了什么?我正在使用Cordova,我假设它能够使用 template 泰格,我没能找到任何说我不能的东西。

    使现代化

    这种情况会发生:

    enter image description here

    当我这么做的时候:

        this.createBoxes().then(function() {
            var template = document.querySelector('#pool');
            var clone = template.cloneNode(true);
            document.querySelector(".app").appendChild(clone);
        });
    

    使用模板。cloneNode成功地移动了 <template> 但这显然不是我想要的,我想得到 <模板> 把他们转移到 .应用程序 容器,而不是整个 <模板> .

    2 回复  |  直到 7 年前
        1
  •  1
  •   Aleks G    7 年前

    如果克隆节点本身有效,那么答案很简单——只需克隆/附加模板的子节点:

    this.createBoxes().then(function() {
        let template = document.querySelector('#pool');
        let app = document.querySelector(".app");
        for(let child of template.childNodes) {
            let clone = child.cloneNode(true);
            app.appendChild(clone);
        }
    });
    

    请注意,我还没有测试这段代码——您可能需要根据需要进行调试。

        2
  •  0
  •   ewizard    7 年前

    我以编程方式向模板添加了一个容器:

        var pool = document.getElementById("pool");
    
        var container = document.createElement("div");
        container.style.flex = "1";
        container.style.backgroundColor = "white";
        container.style.display = "flex";
        container.style.flexDirection = "column";
        container.id = "container";
    
        var row = document.createElement("div");
        row.style.display = "flex";
        row.style.flexDirection = "row";
        row.style.flex = "0.5";
        row.style.justifyContent = "space-around";
        row.style.alignItems = "center";
        row.style.backgroundColor = "green";
        row.className = "row";
    
        container.appendChild(row);
        pool.appendChild(container); 
    

    而不是把我的内容添加到 #pool <template> ,我把它添加到 #container ,然后存储 #容器 节点,然后将其导入到 .app :

    var container_in_temp = document.querySelector('#pool>#container');
    var targetContainer = document.querySelector('.app');
    targetContainer.appendChild(document.importNode(container_in_temp, true));
    

    最后它看起来像这样,里面有一个容器 .应用程序 这实际上是一种更好的结构:)。

    enter image description here

        3
  •  0
  •   Sinus has tentacles    5 年前

    你应该克隆模板的 .content 相反,正如 the documentation .

      var temp = document.getElementsByTagName("template")[0];
      var clon = temp.content.cloneNode(true);
      document.body.appendChild(clon);