将<li>列表与数组合并

时间:2017-12-23 06:00:26

标签: jquery

我创建了一个<ul>列表,并希望使用jQuery将<li>项与数组合并:

&#13;
&#13;
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;
&#13;
&#13;

我想将A分配给第一个<li>B分配给第二个<li>,依此类推......

1 个答案:

答案 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>