如果我有一个清单:
<div class="item active ui-sortable">
<li class="player ui-droppable" data-name="Kev" data-pick-id="534dd2424b65762a89020000">
Teddy Bridgewater
<div class="pull-right" id="player-badges">
<span id="points-holder">
10
</span>
<span class="remove-player player-badge">
<i class="fa fa-trash-o"></i>
</span>
</div>
</li>
<li class="player pick-blank ui-droppable" data-name="" data-pick-id="534dd2424b65762a89030000">
EMPTY
<div class="pull-right" id="player-badges">
<span id="points-holder">
7
</span>
</div>
</li>
<li class="player ui-droppable" data-name="kevin" data-pick-id="534dd2424b65762a89040000">
kevin
<div class="pull-right" id="player-badges">
<span id="points-holder">
5
</span>
<span class="remove-player player-badge">
<i class="fa fa-trash-o"></i>
</span>
</div>
</li>
</div>
我正在制作data-pick-id
数组,然后如何基于数组["534dd2424b65762a89020000", "534dd2424b65762a89030000", "534dd2424b65762a89040000"]
重写此列表?
上下文:我正在使用jquery-sortable并希望仅保留data-pick-id
的dom结构。这样我可以更改所有内容并更新到服务器,同时按ID保留实际的dom位置。
同样,我想做的就是循环并将该数组写入每个data-pick-id
。循环是最好的选择吗?
答案 0 :(得分:1)
一个简单的.each()
就足够了:
var ids = [1, 2, 3];
$('.player').each(function(i) {
$(this).data('pick-id', ids[i]);
});
您可能希望使选择器更加明确。