我有一个方法和以下代码来获取数组标识div
s
arr = $('.card.recipe-slider');
返回我(第一次)
[<div class="card recipe-slider active" data-id="1">…</div>,
<div class="card recipe-slider" data-id="2">…</div>,
<div class="card recipe-slider" data-id="3">…</div>]
但是当我再次调用相同的方法时(第二次),我的Jquery DOM数组会重复,如下所示
[<div class="card recipe-slider active" data-id="1">…</div>,
<div class="card recipe-slider" data-id="2">…</div>,
<div class="card recipe-slider" data-id="3">…</div>
<div class="card recipe-slider" data-id="1">…</div>,
<div class="card recipe-slider" data-id="2">…</div>,
<div class="card recipe-slider" data-id="3">…</div>
]
所以我的问题是如何避免这种重复?
我尝试了以下
1)使数组空白
arr = []
... code
2)获取unique
$.unique($('.card.recipe-slider'))
3)首先清除阵列
$('.card.recipe-slider').empty();
arr = $('.card.recipe-slider')
4)我已经看到了cloning
和replacing
的一些解决方案,我不太清楚,也不知道是否that's the way to go。
完整的代码,差不多...... :),这可以作为一个向导,当用户点击下一个
时,每个div都会显示#html
<div class='card recipe-slider active' data-id='1'>
.. html
<a class="btn btn-warning button-full button-large icon-left next">Next..</a>
</div>
<div class='card recipe-slider' data-id='2'>
.. html
<a class="btn btn-warning button-full button-large icon-left next">Next..</a>
</div>
<div class='card recipe-slider' data-id='3'>
.. html
<a class="btn btn-warning button-full button-large icon-left next">Next..</a>
</div>
JS代码
$(document).on('click', 'a.next', function(e){
arr = [];
arr = $('.card.recipe-slider');
current_card = $(this).closest('.card')[0];
id = current_card.dataset.id;
$(arr[id]).addClass('active');
$(current_card).removeClass('active');
})