【问题标题】:Polymer: Unable to remove child聚合物:无法移除孩子
【发布时间】:2016-12-21 22:44:05
【问题描述】:

我正在尝试将元素动态加载到我的应用程序中,但在删除已导入的元素时遇到了问题。

要导入我的元素,我调用:

openNewElement: function(newElement) {
        this.newElement = newElement;
        var resolvedUrl = this.resolveUrl('newelement-view' + '.html')
        this.importHref(resolvedUrl, null, null, true);
    },

这成功地导入并显示了元素。现在,我尝试使用以下许多不同的实现来删除相同的元素:

closeNewElement: function() {
    Polymer.dom(this).removeChild('newelement-view');
},

但是无论我以不同方式注册节点和/或使用已注册节点的实例调用removeChild(); 函数,我都会不断收到错误消息:

要移除的节点不是该节点的子节点:[object HTMLElement]

在深入研究属性时,我可以看到 newelement-view 已注册为父级的子级,newelement-view 已将正确的父级注册为其父级。我已经尝试了官方聚合物文档中的所有内容以及我可以在堆栈溢出中找到的所有内容,但到目前为止,无济于事。

任何有关如何删除孩子或以更好的方式注册孩子的帮助将不胜感激!

【问题讨论】:

  • 您是否将导入的元素添加到父元素的 dom 中?您发布的代码只有importing元素

标签: javascript node.js polymer getelementbyid removechild


【解决方案1】:

这里有一个例子说明如何做到这一点

<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
<dom-module id="remove-child">
  <template>
    <style></style>
    <div class="firstChild">first chilld</div>
    <p class="secondChild">second chilld</p>
  </template>
</dom-module>
<script>
  Polymer({
    is: 'remove-child',
    attached: function() {
      this.removeChild(this.$$('.secondChild'));
    }
  })
</script>


<remove-child></remove-child>

【讨论】:

  • 这很好用。非常感谢。我错过了 this.$$();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多