将变量推入数组并在每次单击时更新数组?

时间:2015-02-15 21:30:39

标签: javascript jquery arrays

我将一个变量保存到一个数组中,点击一个项目,但每次点击另一个项目时都会改变。目前,这是警告相同的值,而不是新值。

<div class="item">
     <span id="0" class="video" data-vimeoid="http://vimeo.com/XXX"></span>
</div>
<div class="item">
  <span id="1" class="video" data-vimeoid="http://vimeo.com/SSS"></span>
</div>
<div class="item">
  <span id="2" class="video" data-vimeoid="http://vimeo.com/ZZZ"></span>
</div>

然后在jQuery中

var previous = [];
$(document).on( 'click', '.item:not(.is-expanded)', function() {
    var url = videoSpan.data("vimeoid");
    previous.push(url);
    alert(previous[0]);
});

1 个答案:

答案 0 :(得分:2)

你应该做

var previous = [];
$(document).on( 'click', '.item:not(.is-expanded)', function() {
    var url = videoSpan.data("vimeoid");
    previous.push(url);
    alert(previous[previous.length-1]);
});