removeChild,然后再次添加以前删除的Child以重新加载并清理它

removeChild and then add again previously removedChild to reload and clean it?

本文关键字:加载 新加载 Child 然后 添加 删除 removeChild      更新时间:2024-05-05

我试着做一些类似的事情

     var refresh = function(){
                  var elem = document.getElementById(scope.id);
                       elem.parentNode.removeChild(elem);
                    console.log('Canvas Destroyed');
               //Here I would like to add removedChild again so i am sure that is clean//
               };

           scope.$watch('data', function (newVal, oldVal) {
                    if (newVal) {
                        refresh(); //I Use Function Here So i am sure that canvas are empty
                        var newChart = document.getElementById(scope.id).getContext("2d");
                        console.log('Charts0');
                        var PieChart = new Chart(newChart).Pie(scope.data, scope.options);
                        console.log('Charts1');
                    }
                }, true);

问题:是否可以先删除元素,然后将相同的元素放回一个函数中?或者重新加载画布或重新启动,使其在创建图表时没有任何数据。我的应用程序是动态的,如果我不这样做,图表中的一些突出显示层将相互重叠。

这应该可以做到:

function removeAndAdd(el){
    var next = el.nextSibling;
    var parent = el.parentNode;
    parent.removeChild(el);
    //Do stuff to el
    parent.insertBefore(el, next);
}