Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

html - how add elements with they children elements to shadow dom with javascript?

hi i having problem to add elements with they children elements in shadow root:

// in shadow root:
<ul class="breadcrumb">

</ul>

<script>
var number = shadowRoot.host.attributes['number'].nodeValue;
var breadcrumb = shadowRoot.querySelector(".breadcrumb");
var li = document.createElement("li");

for(var i=0; i<number; i++)
{
  breadcrumb.append(li, "text");                        
}

i did this but i only can add one element and no children elements, i want to add this to my ul tag:

<li><slot name="dynamic-value"></slot></li>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

document.createElement creates one LI element, so append(LI) always operates on the same element:

var li = document.createElement("li");
for(var i=0; i<number; i++)
{
  breadcrumb.append(li);                        
}

versus:

for(var i=0; i<number; i++)
{
  var li = document.createElement("li");
  breadcrumb.append(li);                        
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...