How to distribute (insert) content nodes programmatically

北城以北 提交于 2019-12-03 04:03:53
ebidel

Composition is something Shadow DOM is designed for. If that spec bug gets fixed, the best way to do this would be interesting tricks with <content select=":nth-child(...)"> in a <template repeat>. Since you can't (currently) use :nth-child, you could instead use the distributed nodes and data-binding to wrap the content:

<template repeat="{{node in nodes}}">
  <li>
    <html-echo html="{{node.outerHTML}}"></html-echo>
  </li>
</template>
<content id="c" select="*"></content>

nodes is generated from something like:

this.nodes = Array.prototype.slice.call(this.$.c.getDistributedNodes());

I'm using <html-echo> from this post: https://stackoverflow.com/a/22208332/274673

Working demo: http://jsbin.com/mamawugo/2/edit

There is quite old issue at W3C Bugzilla: #18429 - [Shadow]: Specify imperative API for node distribution

But as for now, there is nothing in spec about that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!