用<ul class =“deck”>追加数组

时间:2017-12-25 13:00:22

标签: jquery

有谁能告诉我声明有什么问题。插入分号作为文本/字符串的格式是否正确?

const cardList=["diamond", "fpaper-plane-o", "anchor","bolt","cube","anchor","leaf","bicycle","diamond","bomb","leaf","bomb","bolt","bicycle","paper-plane-o","cube"];

for( let i=0; i<=length; i++){
  jQuery('.deck')('<li class="card"><i class="fa fa-' + cardList[i] + '"></i></li>');
}

1 个答案:

答案 0 :(得分:1)

这是语法错误:

jQuery('.deck')('<li class="card"><i class="fa fa-' + cardList[i] + '"></i></li>');
//------------^^

您必须在此处使用.append()功能:

jQuery('.deck').append('<li class="card"><i class="fa fa-' + cardList[i] + '"></i></li>');

<强>段

const cardList = ["diamond", "paper-plane-o", "anchor", "bolt", "cube", "anchor", "leaf", "bicycle", "diamond", "bomb", "leaf", "bomb", "bolt", "bicycle", "paper-plane-o", "cube"];

for (let i = 0; i < cardList.length; i++) {
  jQuery('.deck').append('<li class="card"><i class="fa fa-' + cardList[i] + '"></i></li>');
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="deck"></ul>

更多更改:

  • 您的for循环变量和条件错误。
  • 其中一个图标类错了。