我创建了一个<ul>
列表,并希望使用jQuery将<li>
项与数组合并:
const cards = ['A', 'B', 'C'];
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card match">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-bolt"></i>
</li>
</ul>
&#13;
我想将A
分配给第一个<li>
,B
分配给第二个<li>
,依此类推......
答案 0 :(得分:0)
检查jquery each参考
const cards = ['A', 'B', 'C','D'];
$('li span').each(function (i) { /*i guess you want to fa fa icons also so added span*/
$(this).text(cards[i]);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i><span></span>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i><span></span>
</li>
<li class="card match">
<i class="fa fa-anchor"></i><span></span>
</li>
<li class="card">
<i class="fa fa-bolt"></i><span></span>
</li>
</ul>