简体   繁体   中英

Polymer 1.x Remove Element By Variable Reference

I have an element that is being injected and stored using the following line:

this._lastPromo = this.$$('#inject-point').appendChild(promo);

I now need to remove this element at a later time in my codebase.I have tried the following 2 scenarios:

this.$$('#inject-point').removeChild(this._lastPromo);

And:

this._lastPromo.parentNode.removeChild(this._lastPromo);

I get the following errors:

Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

And:

Uncaught TypeError: Cannot read property 'removeChild' of null

Question: How can I remove this specific element from the document?

After debugging I discovered that this.$$('#inject-point').appendChild(promo); only returns the newly created DOM object but it does not retain references to it's position in the DOM.

The parent is not recorded on the Object.

To work around the issue I stored the new DOM Object into a property before I inject it into the DOM and then later remove it like so:

Polymer.dom(this._lastPromoEl).parentNode.removeChild(this._lastPromoEl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM